From d51a6753b6e6d0639413b60b74a3b300fdde2c51 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 28 Oct 2021 09:12:29 -0600 Subject: [PATCH 1/7] Add typing_extensions as required dependency --- ci/requirements/environment-windows.yml | 1 + ci/requirements/environment.yml | 1 + ci/requirements/py37-bare-minimum.yml | 1 + ci/requirements/py37-min-all-deps.yml | 1 + ci/requirements/py38-all-but-dask.yml | 1 + doc/getting-started-guide/installing.rst | 1 + requirements.txt | 2 +- setup.cfg | 1 + 8 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ci/requirements/environment-windows.yml b/ci/requirements/environment-windows.yml index 78ead40d5a2..2468ec6267e 100644 --- a/ci/requirements/environment-windows.yml +++ b/ci/requirements/environment-windows.yml @@ -39,6 +39,7 @@ dependencies: - setuptools - sparse - toolz + - typing_extensions - zarr - pip: - numbagg diff --git a/ci/requirements/environment.yml b/ci/requirements/environment.yml index f64ca3677cc..162faa7b74d 100644 --- a/ci/requirements/environment.yml +++ b/ci/requirements/environment.yml @@ -43,6 +43,7 @@ dependencies: - setuptools - sparse - toolz + - typing_extensions - zarr - pip: - numbagg diff --git a/ci/requirements/py37-bare-minimum.yml b/ci/requirements/py37-bare-minimum.yml index 408cf76fdd6..6d7c440261f 100644 --- a/ci/requirements/py37-bare-minimum.yml +++ b/ci/requirements/py37-bare-minimum.yml @@ -13,3 +13,4 @@ dependencies: - numpy=1.17 - pandas=1.0 - setuptools=40.4 + - typing_extensions=3̇.7 diff --git a/ci/requirements/py37-min-all-deps.yml b/ci/requirements/py37-min-all-deps.yml index 7c3230f87b0..c73c5327d3b 100644 --- a/ci/requirements/py37-min-all-deps.yml +++ b/ci/requirements/py37-min-all-deps.yml @@ -47,6 +47,7 @@ dependencies: - setuptools=40.4 - sparse=0.8 - toolz=0.10 + - typing_extensions=3.7 - zarr=2.4 - pip: - numbagg==0.1 diff --git a/ci/requirements/py38-all-but-dask.yml b/ci/requirements/py38-all-but-dask.yml index 3f82990f3b5..688dfb7a2bc 100644 --- a/ci/requirements/py38-all-but-dask.yml +++ b/ci/requirements/py38-all-but-dask.yml @@ -39,6 +39,7 @@ dependencies: - setuptools - sparse - toolz + - typing_extensions - zarr - pip: - numbagg diff --git a/doc/getting-started-guide/installing.rst b/doc/getting-started-guide/installing.rst index 93738da9d9b..5dbef4e401e 100644 --- a/doc/getting-started-guide/installing.rst +++ b/doc/getting-started-guide/installing.rst @@ -8,6 +8,7 @@ Required dependencies - Python (3.7 or later) - setuptools (40.4 or later) +- ``typing_extensions`` (3.7.4.1 or later) - `numpy `__ (1.17 or later) - `pandas `__ (1.0 or later) diff --git a/requirements.txt b/requirements.txt index 732d40cde18..0fa83c8ccc1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,4 @@ numpy >= 1.17 pandas >= 1.0 setuptools >= 40.4 -typing-extensions >= 3.10 +typing-extensions >= 3.7 diff --git a/setup.cfg b/setup.cfg index aa8ca8df0ff..2dc1b7ffeca 100644 --- a/setup.cfg +++ b/setup.cfg @@ -78,6 +78,7 @@ python_requires = >=3.7 install_requires = numpy >= 1.17 pandas >= 1.0 + typing_extensions >= 3.7 setuptools >= 40.4 # For pkg_resources [options.extras_require] From 2418b1e5c8364223c054a4b235ae6c73745b3604 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 28 Oct 2021 09:12:45 -0600 Subject: [PATCH 2/7] Clean up npcompat --- xarray/core/npcompat.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/xarray/core/npcompat.py b/xarray/core/npcompat.py index 6e22c8cf0a4..09f78c5971c 100644 --- a/xarray/core/npcompat.py +++ b/xarray/core/npcompat.py @@ -41,17 +41,10 @@ # fall back for numpy < 1.20, ArrayLike adapted from numpy.typing._array_like if sys.version_info >= (3, 8): from typing import Protocol - - HAVE_PROTOCOL = True else: - try: - from typing_extensions import Protocol - except ImportError: - HAVE_PROTOCOL = False - else: - HAVE_PROTOCOL = True + from typing_extensions import Protocol - if TYPE_CHECKING or HAVE_PROTOCOL: + if TYPE_CHECKING: class _SupportsArray(Protocol): def __array__(self) -> np.ndarray: From 0e4c28d6dc5ef1d1564fcbcb8ece44fb41c95c67 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 28 Oct 2021 09:14:47 -0600 Subject: [PATCH 3/7] Cleanup utils.py --- xarray/core/utils.py | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 77d973f613f..bf4a154560e 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -10,7 +10,6 @@ import warnings from enum import Enum from typing import ( - TYPE_CHECKING, Any, Callable, Collection, @@ -307,35 +306,18 @@ def _is_scalar(value, include_0d): ) -# See GH5624, this is a convoluted way to allow type-checking to use `TypeGuard` without -# requiring typing_extensions as a required dependency to _run_ the code (it is required -# to type-check). -try: - if sys.version_info >= (3, 10): - from typing import TypeGuard - else: - from typing_extensions import TypeGuard -except ImportError: - if TYPE_CHECKING: - raise - else: - - def is_scalar(value: Any, include_0d: bool = True) -> bool: - """Whether to treat a value as a scalar. - - Any non-iterable, string, or 0-D array - """ - return _is_scalar(value, include_0d) - - +if sys.version_info >= (3, 10): + from typing import TypeGuard else: + from typing_extensions import TypeGuard - def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]: - """Whether to treat a value as a scalar. - Any non-iterable, string, or 0-D array - """ - return _is_scalar(value, include_0d) +def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]: + """Whether to treat a value as a scalar. + + Any non-iterable, string, or 0-D array + """ + return _is_scalar(value, include_0d) def is_valid_numpy_dtype(dtype: Any) -> bool: From 7d8e63846c6056390d1f8f0c66c65fcce2c76b43 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 28 Oct 2021 09:19:01 -0600 Subject: [PATCH 4/7] Cleanup options.py --- xarray/core/options.py | 83 ++++++++++++------------------------------ 1 file changed, 24 insertions(+), 59 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 14f77306316..c9e037e6fd6 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -6,70 +6,35 @@ # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): from typing import TYPE_CHECKING, Literal, TypedDict, Union +else: + from typing import TYPE_CHECKING, Union - if TYPE_CHECKING: - try: - from matplotlib.colors import Colormap - except ImportError: - Colormap = str - - class T_Options(TypedDict): - arithmetic_join: Literal["inner", "outer", "left", "right", "exact"] - cmap_divergent: Union[str, "Colormap"] - cmap_sequential: Union[str, "Colormap"] - display_max_rows: int - display_style: Literal["text", "html"] - display_width: int - display_expand_attrs: Literal["default", True, False] - display_expand_coords: Literal["default", True, False] - display_expand_data_vars: Literal["default", True, False] - display_expand_data: Literal["default", True, False] - enable_cftimeindex: bool - file_cache_maxsize: int - keep_attrs: Literal["default", True, False] - warn_for_unclosed_files: bool - use_bottleneck: bool + from typing_extensions import Literal, TypedDict -else: - # See GH5624, this is a convoluted way to allow type-checking to use - # `TypedDict` and `Literal` without requiring typing_extensions as a - # required dependency to _run_ the code (it is required to type-check). +if TYPE_CHECKING: try: - from typing import TYPE_CHECKING, Union - - from typing_extensions import Literal, TypedDict - - if TYPE_CHECKING: - try: - from matplotlib.colors import Colormap - except ImportError: - Colormap = str - - class T_Options(TypedDict): - arithmetic_join: Literal["inner", "outer", "left", "right", "exact"] - cmap_divergent: Union[str, "Colormap"] - cmap_sequential: Union[str, "Colormap"] - display_max_rows: int - display_style: Literal["text", "html"] - display_width: int - display_expand_attrs: Literal["default", True, False] - display_expand_coords: Literal["default", True, False] - display_expand_data_vars: Literal["default", True, False] - display_expand_data: Literal["default", True, False] - enable_cftimeindex: bool - file_cache_maxsize: int - keep_attrs: Literal["default", True, False] - warn_for_unclosed_files: bool - use_bottleneck: bool - + from matplotlib.colors import Colormap except ImportError: - from typing import TYPE_CHECKING, Any, Dict, Hashable - - if TYPE_CHECKING: - raise - else: - T_Options = Dict[Hashable, Any] + Colormap = str + + +class T_Options(TypedDict): + arithmetic_join: Literal["inner", "outer", "left", "right", "exact"] + cmap_divergent: Union[str, "Colormap"] + cmap_sequential: Union[str, "Colormap"] + display_max_rows: int + display_style: Literal["text", "html"] + display_width: int + display_expand_attrs: Literal["default", True, False] + display_expand_coords: Literal["default", True, False] + display_expand_data_vars: Literal["default", True, False] + display_expand_data: Literal["default", True, False] + enable_cftimeindex: bool + file_cache_maxsize: int + keep_attrs: Literal["default", True, False] + warn_for_unclosed_files: bool + use_bottleneck: bool OPTIONS: T_Options = { From 461c0cf9fe9d80b9d188fda634b67dcb1382d9f8 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Thu, 28 Oct 2021 20:50:51 +0530 Subject: [PATCH 5/7] Update doc/getting-started-guide/installing.rst --- doc/getting-started-guide/installing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/getting-started-guide/installing.rst b/doc/getting-started-guide/installing.rst index 5dbef4e401e..c6bc84e6ddb 100644 --- a/doc/getting-started-guide/installing.rst +++ b/doc/getting-started-guide/installing.rst @@ -8,7 +8,7 @@ Required dependencies - Python (3.7 or later) - setuptools (40.4 or later) -- ``typing_extensions`` (3.7.4.1 or later) +- ``typing_extensions`` (3.7 or later) - `numpy `__ (1.17 or later) - `pandas `__ (1.0 or later) From 6c6775a97a7c6e617d81b018c44c7da01cf8db38 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 28 Oct 2021 10:23:32 -0600 Subject: [PATCH 6/7] Revert "Cleanup utils.py" This reverts commit 0e4c28d6dc5ef1d1564fcbcb8ece44fb41c95c67. --- xarray/core/utils.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/xarray/core/utils.py b/xarray/core/utils.py index bf4a154560e..77d973f613f 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -10,6 +10,7 @@ import warnings from enum import Enum from typing import ( + TYPE_CHECKING, Any, Callable, Collection, @@ -306,18 +307,35 @@ def _is_scalar(value, include_0d): ) -if sys.version_info >= (3, 10): - from typing import TypeGuard -else: - from typing_extensions import TypeGuard +# See GH5624, this is a convoluted way to allow type-checking to use `TypeGuard` without +# requiring typing_extensions as a required dependency to _run_ the code (it is required +# to type-check). +try: + if sys.version_info >= (3, 10): + from typing import TypeGuard + else: + from typing_extensions import TypeGuard +except ImportError: + if TYPE_CHECKING: + raise + else: + def is_scalar(value: Any, include_0d: bool = True) -> bool: + """Whether to treat a value as a scalar. -def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]: - """Whether to treat a value as a scalar. + Any non-iterable, string, or 0-D array + """ + return _is_scalar(value, include_0d) - Any non-iterable, string, or 0-D array - """ - return _is_scalar(value, include_0d) + +else: + + def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]: + """Whether to treat a value as a scalar. + + Any non-iterable, string, or 0-D array + """ + return _is_scalar(value, include_0d) def is_valid_numpy_dtype(dtype: Any) -> bool: From 0a76c0ad34875df83400eb1044538fa936aed49e Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Thu, 28 Oct 2021 22:01:44 +0530 Subject: [PATCH 7/7] Update ci/requirements/py37-bare-minimum.yml --- ci/requirements/py37-bare-minimum.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/requirements/py37-bare-minimum.yml b/ci/requirements/py37-bare-minimum.yml index 6d7c440261f..0cecf885436 100644 --- a/ci/requirements/py37-bare-minimum.yml +++ b/ci/requirements/py37-bare-minimum.yml @@ -13,4 +13,4 @@ dependencies: - numpy=1.17 - pandas=1.0 - setuptools=40.4 - - typing_extensions=3̇.7 + - typing_extensions=3.7