Skip to content

Commit deaca14

Browse files
authored
Make typing-extensions optional (#5624)
1 parent 92cb751 commit deaca14

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ repos:
4343
types-pytz,
4444
# Dependencies that are typed
4545
numpy,
46+
typing-extensions==3.10.0.0,
4647
]
4748
# run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
4849
# - repo: https://github.com/asottile/pyupgrade

doc/getting-started-guide/installing.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Required dependencies
88

99
- Python (3.7 or later)
1010
- setuptools (40.4 or later)
11-
- typing-extensions (3.10 or later)
1211
- `numpy <http://www.numpy.org/>`__ (1.17 or later)
1312
- `pandas <http://pandas.pydata.org/>`__ (1.0 or later)
1413

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ classifiers =
6767
Programming Language :: Python :: 3.7
6868
Programming Language :: Python :: 3.8
6969
Programming Language :: Python :: 3.9
70+
Programming Language :: Python :: 3.10
7071
Topic :: Scientific/Engineering
7172

7273
[options]
@@ -78,7 +79,6 @@ install_requires =
7879
numpy >= 1.17
7980
pandas >= 1.0
8081
setuptools >= 40.4 # For pkg_resources
81-
typing-extensions >= 3.10 # Backported type hints
8282

8383
[options.extras_require]
8484
io =

xarray/core/utils.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import warnings
1111
from enum import Enum
1212
from typing import (
13+
TYPE_CHECKING,
1314
Any,
1415
Callable,
1516
Collection,
@@ -32,12 +33,6 @@
3233
import numpy as np
3334
import pandas as pd
3435

35-
if sys.version_info >= (3, 10):
36-
from typing import TypeGuard
37-
else:
38-
from typing_extensions import TypeGuard
39-
40-
4136
K = TypeVar("K")
4237
V = TypeVar("V")
4338
T = TypeVar("T")
@@ -297,11 +292,7 @@ def either_dict_or_kwargs(
297292
return pos_kwargs
298293

299294

300-
def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]:
301-
"""Whether to treat a value as a scalar.
302-
303-
Any non-iterable, string, or 0-D array
304-
"""
295+
def _is_scalar(value, include_0d):
305296
from .variable import NON_NUMPY_SUPPORTED_ARRAY_TYPES
306297

307298
if include_0d:
@@ -316,6 +307,37 @@ def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]:
316307
)
317308

318309

310+
# See GH5624, this is a convoluted way to allow type-checking to use `TypeGuard` without
311+
# requiring typing_extensions as a required dependency to _run_ the code (it is required
312+
# to type-check).
313+
try:
314+
if sys.version_info >= (3, 10):
315+
from typing import TypeGuard
316+
else:
317+
from typing_extensions import TypeGuard
318+
except ImportError:
319+
if TYPE_CHECKING:
320+
raise
321+
else:
322+
323+
def is_scalar(value: Any, include_0d: bool = True) -> bool:
324+
"""Whether to treat a value as a scalar.
325+
326+
Any non-iterable, string, or 0-D array
327+
"""
328+
return _is_scalar(value, include_0d)
329+
330+
331+
else:
332+
333+
def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]:
334+
"""Whether to treat a value as a scalar.
335+
336+
Any non-iterable, string, or 0-D array
337+
"""
338+
return _is_scalar(value, include_0d)
339+
340+
319341
def is_valid_numpy_dtype(dtype: Any) -> bool:
320342
try:
321343
np.dtype(dtype)

0 commit comments

Comments
 (0)