Skip to content

Commit e07da06

Browse files
headtr1ckIllviljan
andauthored
Fix numpy 1.20 incompatibility (#6821)
* use custom _SupportsDType definition * update whats-new * Test CI with numpy 1.20 * Update ci/requirements/environment.yml Co-authored-by: Illviljan <[email protected]>
1 parent 3f7cc2d commit e07da06

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ Bug fixes
3838
(:issue:`6826`, :pull:`6832`)
3939
By `Jimmy Westling <https://github.com/illviljan>`_.
4040
- :py:attr:`DataArray.nbytes` now uses the ``nbytes`` property of the underlying array if available.
41+
(:pull:`6797`)
4142
By `Max Jones <https://github.com/maxrjones>`_.
43+
- Fix incompatibility with numpy 1.20 (:issue:`6818`, :pull:`6821`)
44+
By `Michael Niklas <https://github.com/headtr1ck>`_.
4245

4346
Documentation
4447
~~~~~~~~~~~~~

xarray/core/npcompat.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
Any,
3434
List,
3535
Literal,
36+
Protocol,
3637
Sequence,
3738
Tuple,
3839
Type,
@@ -43,10 +44,26 @@
4344
import numpy as np
4445
from packaging.version import Version
4546

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+
4663
# Type annotations stubs
4764
try:
4865
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
5067

5168
# Xarray requires a Mapping[Hashable, dtype] in many places which
5269
# conflics with numpys own DTypeLike (with dtypes for fields).
@@ -69,27 +86,10 @@
6986
# because numpy does the same?
7087
List[Any],
7188
# anything with a dtype attribute
72-
_SupportsDType[np.dtype],
89+
_SupportsDType,
7390
]
7491
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
9393
_T = TypeVar("_T")
9494
_NestedSequence = Union[
9595
_T,
@@ -120,7 +120,7 @@ def dtype(self) -> np.dtype:
120120
Type[Any],
121121
Tuple[Any, Any],
122122
List[Any],
123-
_SupportsDTypeFallback,
123+
_SupportsDType,
124124
]
125125
DTypeLike = DTypeLikeSave # type: ignore[misc]
126126

0 commit comments

Comments
 (0)