make mypy work again
MODIFY
.pre-commit-config.yaml
.pre-commit-config.yaml
@@ -27,6 +27,12 @@ repos:
hooks:
- id: seed-isort-config
+- repo: https://github.com/pre-commit/mirrors-mypy
+ rev: 'v0.812'
+ hooks:
+ - id: mypy
+ exclude: tests
+
- repo: https://github.com/pycqa/isort
rev: 5.5.4
hooks:

MODIFY
pangeo_forge/patterns.py
pangeo_forge/patterns.py
@@ -46,7 +46,7 @@ class MergeDim:
keys: Sequence[Any] = field(repr=False)
-Index = Tuple[int]
+Index = Tuple[int, ...]
class FilePattern:
@@ -62,9 +62,7 @@ class FilePattern:
product of the keys is used to generate the full list of file paths.
"""
- def __init__(
- self, format_function: Callable, *combine_dims: Sequence[Union[MergeDim, ConcatDim]]
- ):
+ def __init__(self, format_function: Callable, *combine_dims: Union[MergeDim, ConcatDim]):
self.format_function = format_function
self.combine_dims = combine_dims
@@ -75,10 +73,12 @@ class FilePattern:
kwargs = dict(zip(dim_names, keys))
fnames.append(format_function(**kwargs))
shape = [len(cdim.keys) for cdim in combine_dims]
- fnames = np.array(fnames)
- fnames.shape = shape
+ fnames_np = np.array(fnames)
+ fnames_np.shape = tuple(shape)
+ # This way of defining coords is incompatible with xarray type annotations.
+ # I don't understand why.
coords = {cdim.name: (cdim.name, cdim.keys) for cdim in combine_dims}
- self._da = xr.DataArray(fnames, dims=coords, coords=coords)
+ self._da = xr.DataArray(fnames_np, dims=list(coords), coords=coords) # type: ignore
@property
def dims(self) -> Dict[str, int]:
@@ -87,7 +87,7 @@ class FilePattern:
return {op.name: len(op.keys) for op in self.combine_dims}
@property
- def shape(self) -> Tuple[int]:
+ def shape(self) -> Tuple[int, ...]:
"""Shape of the filename matrix."""
return self._da.shape
@@ -102,9 +102,9 @@ class FilePattern:
return [op.name for op in self.combine_dims if isinstance(op, ConcatDim)]
@property
- def nitems_per_input(self) -> Dict[str, Optional[int]]:
+ def nitems_per_input(self) -> Dict[str, Union[int, None]]:
"""Dictionary mapping concat dims to number of items per file."""
- nitems = {}
+ nitems = {} # type: Dict[str, Union[int, None]]
for op in self.combine_dims:
if isinstance(op, ConcatDim):
if op.nitems_per_file:

MODIFY
pangeo_forge/recipe.py
pangeo_forge/recipe.py
@@ -484,7 +484,7 @@ class XarrayZarrRecipe(BaseRecipe):
yield fp
except (IOError, FileNotFoundError, UninitializedTargetError) as err:
if self.cache_inputs:
- raise err(
+ raise err( # type: ignore
f"You are trying to open input {fname}, but the file is "
"not cached yet. First call `cache_input` or set "
"`cache_inputs=False`."
@@ -588,7 +588,7 @@ class XarrayZarrRecipe(BaseRecipe):
if self._nitems_per_input:
input_sequence_lens = (self._nitems_per_input,) * self.file_pattern.dims[
- self._concat_dim
+ self._concat_dim # type: ignore
]
else:
input_sequence_lens = json.loads(
@@ -596,7 +596,7 @@ class XarrayZarrRecipe(BaseRecipe):
)["input_sequence_lens"]
chunk_bounds, all_chunk_conflicts = chunk_bounds_and_conflicts(
- input_sequence_lens, self._concat_dim_chunks
+ input_sequence_lens, self._concat_dim_chunks # type: ignore
)
input_positions = [self.input_position(input_key) for input_key in input_keys]
start = chunk_bounds[min(input_positions)]

MODIFY
setup.cfg
setup.cfg
@@ -15,17 +15,15 @@ log_cli = False
# remove this once rechunker executors are factored into a standalone package
# that exports type hints (https://mypy.readthedocs.io/en/latest/installed_packages.html#installed-packages)
-[mypy-rechunker]
-ignore_missing_imports = True
-[mypy-numpy]
-ignore_missing_imports = True
+[mypy]
+show_error_codes = True
-[mypy-dask.distributed]
+[mypy-dask.*]
ignore_missing_imports = True
-
-[mypy-fsspec]
+[mypy-rechunker.*]
ignore_missing_imports = True
-
-[mypy-zarr]
+[mypy-fsspec.*]
+ignore_missing_imports = True
+[mypy-zarr.*]
ignore_missing_imports = True

make mypy work again