|
33 | 33 | Any,
|
34 | 34 | List,
|
35 | 35 | Literal,
|
| 36 | + Protocol, |
36 | 37 | Sequence,
|
37 | 38 | Tuple,
|
38 | 39 | Type,
|
|
43 | 44 | import numpy as np
|
44 | 45 | from packaging.version import Version
|
45 | 46 |
|
| 47 | +if TYPE_CHECKING: |
| 48 | + |
| 49 | + class _SupportsArray(Protocol): |
| 50 | + def __array__(self) -> np.ndarray: |
| 51 | + ... |
| 52 | + |
| 53 | + # once NumPy 1.21 is minimum version, use NumPys definition directly |
| 54 | + class _SupportsDType(Protocol): |
| 55 | + @property |
| 56 | + def dtype(self) -> np.dtype: |
| 57 | + ... |
| 58 | + |
| 59 | +else: |
| 60 | + _SupportsArray = Any |
| 61 | + _SupportsDType = Any |
| 62 | + |
46 | 63 | # Type annotations stubs
|
47 | 64 | try:
|
48 | 65 | from numpy.typing import ArrayLike, DTypeLike
|
49 |
| - from numpy.typing._dtype_like import _DTypeLikeNested, _ShapeLike, _SupportsDType |
| 66 | + from numpy.typing._dtype_like import _DTypeLikeNested, _ShapeLike |
50 | 67 |
|
51 | 68 | # Xarray requires a Mapping[Hashable, dtype] in many places which
|
52 | 69 | # conflics with numpys own DTypeLike (with dtypes for fields).
|
|
69 | 86 | # because numpy does the same?
|
70 | 87 | List[Any],
|
71 | 88 | # anything with a dtype attribute
|
72 |
| - _SupportsDType[np.dtype], |
| 89 | + _SupportsDType, |
73 | 90 | ]
|
74 | 91 | except ImportError:
|
75 |
| - # fall back for numpy < 1.20, ArrayLike adapted from numpy.typing._array_like |
76 |
| - from typing import Protocol |
77 |
| - |
78 |
| - if TYPE_CHECKING: |
79 |
| - |
80 |
| - class _SupportsArray(Protocol): |
81 |
| - def __array__(self) -> np.ndarray: |
82 |
| - ... |
83 |
| - |
84 |
| - class _SupportsDTypeFallback(Protocol): |
85 |
| - @property |
86 |
| - def dtype(self) -> np.dtype: |
87 |
| - ... |
88 |
| - |
89 |
| - else: |
90 |
| - _SupportsArray = Any |
91 |
| - _SupportsDTypeFallback = Any |
92 |
| - |
| 92 | + # fall back for numpy < 1.20 |
93 | 93 | _T = TypeVar("_T")
|
94 | 94 | _NestedSequence = Union[
|
95 | 95 | _T,
|
@@ -120,7 +120,7 @@ def dtype(self) -> np.dtype:
|
120 | 120 | Type[Any],
|
121 | 121 | Tuple[Any, Any],
|
122 | 122 | List[Any],
|
123 |
| - _SupportsDTypeFallback, |
| 123 | + _SupportsDType, |
124 | 124 | ]
|
125 | 125 | DTypeLike = DTypeLikeSave # type: ignore[misc]
|
126 | 126 |
|
|
0 commit comments