Skip to content

Commit 23492bc

Browse files
author
Joseph Hamman
committed
fix numpy typing import
1 parent d9b9ee9 commit 23492bc

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
xarray>=0.16
2+
numpy>=1.20

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ skip=
2121
[tool:pytest]
2222
log_cli = True
2323
log_level = INFO
24+
25+
[mypy]
26+
plugins = numpy.typing.mypy_plugin

xarray_schema/base.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import json
22
from abc import abstractmethod
3-
from typing import Any, Dict, Hashable, Tuple, Union
4-
5-
DimsT = Tuple[Union[Hashable, None]]
6-
ShapeT = Tuple[Union[int, None]]
7-
ChunksT = Union[bool, Dict[Hashable, Union[int, None]]]
3+
from typing import Any, Dict
84

95

106
class SchemaError(Exception):

xarray_schema/components.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
from collections.abc import Iterable
2-
from typing import Any, Dict, Hashable, Mapping, Optional, Tuple, Union
2+
from typing import Any, Dict, Hashable, Mapping, Tuple, Union
33

44
import numpy as np
5-
import numpy.typing as npt
65

76
from .base import BaseSchema, SchemaError
8-
from .types import ChunksT, DimsT, ShapeT
7+
from .types import ChunksT, DimsT, DTypeLike, ShapeT
98

109

1110
class DTypeSchema(BaseSchema):
1211

1312
_json_schema = {'type': 'string'}
1413

15-
def __init__(self, dtype: npt.DTypeLike) -> None:
14+
def __init__(self, dtype: DTypeLike) -> None:
1615
if dtype in [np.floating, np.integer, np.signedinteger, np.unsignedinteger, np.generic]:
1716
self.dtype = dtype
1817
else:
1918
self.dtype = np.dtype(dtype)
2019

21-
def validate(self, dtype: npt.DTypeLike) -> None:
20+
def validate(self, dtype: DTypeLike) -> None:
2221
'''Validate dtype
2322
2423
Parameters
@@ -127,9 +126,7 @@ class ChunksSchema(BaseSchema):
127126
def __init__(self, chunks: ChunksT) -> None:
128127
self.chunks = chunks
129128

130-
def validate(
131-
self, chunks: Optional[Tuple[Tuple[int, ...], ...]], dims: tuple, shape: tuple
132-
) -> None:
129+
def validate(self, chunks: Tuple[Tuple[int, ...], ...], dims: tuple, shape: tuple) -> None:
133130
'''Validate chunks
134131
135132
Parameters

xarray_schema/dataarray.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Any, Callable, Dict, List, Mapping, Union
22

3-
import numpy as np
43
import xarray as xr
54

65
from .base import BaseSchema, SchemaError
@@ -13,15 +12,15 @@
1312
NameSchema,
1413
ShapeSchema,
1514
)
16-
from .types import ChunksT, DimsT, ShapeT
15+
from .types import ChunksT, DimsT, DTypeLike, ShapeT
1716

1817

1918
class DataArraySchema(BaseSchema):
2019
'''A light-weight xarray.DataArray validator
2120
2221
Parameters
2322
----------
24-
dtype : np.typing.DTypeLike or DTypeSchema, optional
23+
dtype : DTypeLike or DTypeSchema, optional
2524
Datatype of the the variable. If a string is specified it must be a valid NumPy data type value, by default None
2625
shape : ShapeT or ShapeSchema, optional
2726
Shape of the DataArray. `None` may be used as a wildcard value. By default None
@@ -51,7 +50,7 @@ class DataArraySchema(BaseSchema):
5150

5251
def __init__(
5352
self,
54-
dtype: Union[np.typing.DTypeLike, DTypeSchema] = None,
53+
dtype: Union[DTypeLike, DTypeSchema] = None,
5554
shape: Union[ShapeT, ShapeSchema] = None,
5655
dims: Union[DimsT, DimsSchema] = None,
5756
name: Union[str, NameSchema] = None,
@@ -77,7 +76,7 @@ def dtype(self) -> Union[DTypeSchema, None]:
7776
return self._dtype
7877

7978
@dtype.setter
80-
def dtype(self, value: Union[DTypeSchema, np.typing.DTypeLike, None]):
79+
def dtype(self, value: Union[DTypeSchema, DTypeLike, None]):
8180
if value is None or isinstance(value, DTypeSchema):
8281
self._dtype = value
8382
else:

xarray_schema/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from typing import Dict, Tuple, Union
22

3+
import numpy as np
4+
5+
DTypeLike = np.typing.DTypeLike
36
DimsT = Tuple[Union[str, None]]
47
ShapeT = Tuple[Union[int, None]]
58
ChunksT = Union[bool, Dict[str, Union[int, None]]]

0 commit comments

Comments
 (0)