Infer return object by running the function on meta objects.
(
func: Callable[..., T_Xarray], obj: DataArray | Dataset, *args, **kwargs
)
| 117 | |
| 118 | |
| 119 | def infer_template( |
| 120 | func: Callable[..., T_Xarray], obj: DataArray | Dataset, *args, **kwargs |
| 121 | ) -> T_Xarray: |
| 122 | """Infer return object by running the function on meta objects.""" |
| 123 | meta_args = [make_meta(arg) for arg in (obj,) + args] |
| 124 | |
| 125 | try: |
| 126 | template = func(*meta_args, **kwargs) |
| 127 | except Exception as e: |
| 128 | raise Exception( |
| 129 | "Cannot infer object returned from running user provided function. " |
| 130 | "Please supply the 'template' kwarg to map_blocks." |
| 131 | ) from e |
| 132 | |
| 133 | if not isinstance(template, Dataset | DataArray): |
| 134 | raise TypeError( |
| 135 | "Function must return an xarray DataArray or Dataset. Instead it returned " |
| 136 | f"{type(template)}" |
| 137 | ) |
| 138 | |
| 139 | return template |
| 140 | |
| 141 | |
| 142 | def make_dict(x: DataArray | Dataset) -> dict[Hashable, Any]: |
no test coverage detected
searching dependent graphs…