From be3b07b88500a90e3eace0234405ad1cab155809 Mon Sep 17 00:00:00 2001 From: Michael Niklas Date: Sun, 24 Jul 2022 12:38:43 +0200 Subject: [PATCH 1/4] use custom _SupportsDType definition --- xarray/core/npcompat.py | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/xarray/core/npcompat.py b/xarray/core/npcompat.py index 85a8f88aba6..3f57a0822a4 100644 --- a/xarray/core/npcompat.py +++ b/xarray/core/npcompat.py @@ -33,6 +33,7 @@ Any, List, Literal, + Protocol, Sequence, Tuple, Type, @@ -43,10 +44,26 @@ import numpy as np from packaging.version import Version +if TYPE_CHECKING: + + class _SupportsArray(Protocol): + def __array__(self) -> np.ndarray: + ... + + # once NumPy 1.21 is minimum version, use NumPys definition directly + class _SupportsDType(Protocol): + @property + def dtype(self) -> np.dtype: + ... + +else: + _SupportsArray = Any + _SupportsDType = Any + # Type annotations stubs try: from numpy.typing import ArrayLike, DTypeLike - from numpy.typing._dtype_like import _DTypeLikeNested, _ShapeLike, _SupportsDType + from numpy.typing._dtype_like import _DTypeLikeNested, _ShapeLike # Xarray requires a Mapping[Hashable, dtype] in many places which # conflics with numpys own DTypeLike (with dtypes for fields). @@ -69,27 +86,10 @@ # because numpy does the same? List[Any], # anything with a dtype attribute - _SupportsDType[np.dtype], + _SupportsDType, ] except ImportError: - # fall back for numpy < 1.20, ArrayLike adapted from numpy.typing._array_like - from typing import Protocol - - if TYPE_CHECKING: - - class _SupportsArray(Protocol): - def __array__(self) -> np.ndarray: - ... - - class _SupportsDTypeFallback(Protocol): - @property - def dtype(self) -> np.dtype: - ... - - else: - _SupportsArray = Any - _SupportsDTypeFallback = Any - + # fall back for numpy < 1.20 _T = TypeVar("_T") _NestedSequence = Union[ _T, @@ -120,7 +120,7 @@ def dtype(self) -> np.dtype: Type[Any], Tuple[Any, Any], List[Any], - _SupportsDTypeFallback, + _SupportsDType, ] DTypeLike = DTypeLikeSave # type: ignore[misc] From 870d6226943694f0c5db4ddb65e6a3b65f5fcec5 Mon Sep 17 00:00:00 2001 From: Michael Niklas Date: Sun, 24 Jul 2022 19:15:54 +0200 Subject: [PATCH 2/4] update whats-new --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 67f697597cf..5de26fa2a93 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -35,7 +35,10 @@ Bug fixes ~~~~~~~~~ - :py:attr:`DataArray.nbytes` now uses the ``nbytes`` property of the underlying array if available. + (:pull:`6797`) By `Max Jones `_. +- Fix incompatibility with numpy 1.20 (:issue:`6818`, :pull:`6821`) + By `Michael Niklas `_. Documentation ~~~~~~~~~~~~~ From 4de32fefb35133dfd2133dd997193b24c0064dc1 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 24 Jul 2022 22:39:53 +0200 Subject: [PATCH 3/4] Test CI with numpy 1.20 --- ci/requirements/environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/requirements/environment.yml b/ci/requirements/environment.yml index d37bb7dc44a..85df0050869 100644 --- a/ci/requirements/environment.yml +++ b/ci/requirements/environment.yml @@ -25,7 +25,7 @@ dependencies: - netcdf4 - numba - numexpr - - numpy + - numpy=1.20 - packaging - pandas - pint From 34cf6a46736f28885d538993dab00810998bd455 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 28 Jul 2022 21:20:59 +0200 Subject: [PATCH 4/4] Update ci/requirements/environment.yml --- ci/requirements/environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/requirements/environment.yml b/ci/requirements/environment.yml index 85df0050869..d37bb7dc44a 100644 --- a/ci/requirements/environment.yml +++ b/ci/requirements/environment.yml @@ -25,7 +25,7 @@ dependencies: - netcdf4 - numba - numexpr - - numpy=1.20 + - numpy - packaging - pandas - pint