Skip to content

Commit b9750cf

Browse files
committed
#152 Update generic type hints
1 parent 1555e1d commit b9750cf

File tree

6 files changed

+18
-27
lines changed

6 files changed

+18
-27
lines changed

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ pytest = "^7.1"
3636
sphinx = "^4.5"
3737

3838
[tool.pyright]
39-
typeCheckingMode = "strict"
4039
reportImportCycles = "warning"
4140
reportUnknownArgumentType = "warning"
4241
reportUnknownMemberType = "warning"
43-
reportUnknownParameterType = "warning"
44-
reportUnknownVariableType = "warning"
42+
typeCheckingMode = "strict"
4543

4644
[build-system]
4745
requires = ["poetry-core>=1.0.0"]

xarray_dataclasses/__init__.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22
__version__ = "1.1.0"
33

44

5-
# for Python 3.7 and 3.8
6-
def _make_field_generic():
7-
from dataclasses import Field
8-
from typing import Sequence
9-
10-
GenericAlias = type(Sequence[int])
11-
Field.__class_getitem__ = classmethod(GenericAlias)
12-
13-
14-
_make_field_generic()
15-
16-
175
# submodules
186
from . import dataarray
197
from . import dataset

xarray_dataclasses/dataarray.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# submodules
2828
from .datamodel import DataModel
2929
from .dataoptions import DataOptions
30-
from .typing import DataClass, DataClassFields, DataType, Order, Shape, Sizes
30+
from .typing import AnyArray, DataClass, DataClassFields, DataType, Order, Shape, Sizes
3131

3232

3333
# type hints
@@ -168,7 +168,7 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
168168
@classmethod
169169
def shaped(
170170
cls: Type[OptionedClass[PInit, TDataArray]],
171-
func: Callable[[Shape], np.ndarray],
171+
func: Callable[[Shape], AnyArray],
172172
shape: Union[Shape, Sizes],
173173
**kwargs: Any,
174174
) -> TDataArray:
@@ -178,7 +178,7 @@ def shaped(
178178
@classmethod
179179
def shaped(
180180
cls: Type[DataClass[PInit]],
181-
func: Callable[[Shape], np.ndarray],
181+
func: Callable[[Shape], AnyArray],
182182
shape: Union[Shape, Sizes],
183183
**kwargs: Any,
184184
) -> xr.DataArray:
@@ -187,7 +187,7 @@ def shaped(
187187
@classmethod
188188
def shaped(
189189
cls: Any,
190-
func: Callable[[Shape], np.ndarray],
190+
func: Callable[[Shape], AnyArray],
191191
shape: Union[Shape, Sizes],
192192
**kwargs: Any,
193193
) -> Any:

xarray_dataclasses/datamodel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
# standard library
6-
from dataclasses import Field, dataclass, field, is_dataclass
6+
from dataclasses import dataclass, field, is_dataclass
77
from typing import Any, Dict, Hashable, List, Optional, Tuple, Type, Union, cast
88

99

@@ -15,6 +15,7 @@
1515

1616
# submodules
1717
from .typing import (
18+
AnyField,
1819
DataClass,
1920
DataType,
2021
Dims,
@@ -204,7 +205,7 @@ def eval_dataclass(dataclass: AnyDataClass[PInit]) -> None:
204205
field.type = types[field.name]
205206

206207

207-
def get_entry(field: Field[Any], value: Any) -> AnyEntry:
208+
def get_entry(field: AnyField, value: Any) -> AnyEntry:
208209
"""Create an entry from a field and its value."""
209210
field_type = get_field_type(field.type)
210211
repr_type = get_repr_type(field.type)

xarray_dataclasses/dataset.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# submodules
1919
from .datamodel import DataModel
2020
from .dataoptions import DataOptions
21-
from .typing import DataClass, DataClassFields, DataType, Order, Shape, Sizes
21+
from .typing import AnyArray, DataClass, DataClassFields, DataType, Order, Shape, Sizes
2222

2323

2424
# type hints
@@ -159,7 +159,7 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
159159
@classmethod
160160
def shaped(
161161
cls: Type[OptionedClass[PInit, TDataset]],
162-
func: Callable[[Shape], np.ndarray],
162+
func: Callable[[Shape], AnyArray],
163163
sizes: Sizes,
164164
**kwargs: Any,
165165
) -> TDataset:
@@ -169,7 +169,7 @@ def shaped(
169169
@classmethod
170170
def shaped(
171171
cls: Type[DataClass[PInit]],
172-
func: Callable[[Shape], np.ndarray],
172+
func: Callable[[Shape], AnyArray],
173173
sizes: Sizes,
174174
**kwargs: Any,
175175
) -> xr.Dataset:
@@ -178,7 +178,7 @@ def shaped(
178178
@classmethod
179179
def shaped(
180180
cls: Any,
181-
func: Callable[[Shape], np.ndarray],
181+
func: Callable[[Shape], AnyArray],
182182
sizes: Sizes,
183183
**kwargs: Any,
184184
) -> Any:
@@ -194,7 +194,7 @@ def shaped(
194194
195195
"""
196196
model = DataModel.from_dataclass(cls)
197-
data_vars: Dict[str, np.ndarray] = {}
197+
data_vars: Dict[str, AnyArray] = {}
198198

199199
for key, entry in model.data_vars_items:
200200
shape = tuple(sizes[dim] for dim in entry.dims)

xarray_dataclasses/typing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535

3636

3737
# dependencies
38+
import numpy as np
3839
import xarray as xr
3940
from more_itertools import collapse
4041
from typing_extensions import (
4142
Annotated,
4243
Literal,
4344
ParamSpec,
4445
Protocol,
46+
TypeAlias,
4547
get_args,
4648
get_origin,
4749
get_type_hints,
@@ -56,7 +58,9 @@
5658
TDtype = TypeVar("TDtype", covariant=True)
5759
TName = TypeVar("TName", bound=Hashable)
5860

59-
DataClassFields = Dict[str, Field[Any]]
61+
AnyArray: TypeAlias = "np.ndarray[Any, Any]"
62+
AnyField: TypeAlias = "Field[Any]"
63+
DataClassFields = Dict[str, AnyField]
6064
DataType = Union[xr.DataArray, xr.Dataset]
6165
Dims = Tuple[str, ...]
6266
Dtype = Optional[str]

0 commit comments

Comments
 (0)