Skip to content

Commit 6c6775a

Browse files
committed
Revert "Cleanup utils.py"
This reverts commit 0e4c28d.
1 parent 7d8e638 commit 6c6775a

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

xarray/core/utils.py

Lines changed: 27 additions & 9 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,
@@ -306,18 +307,35 @@ def _is_scalar(value, include_0d):
306307
)
307308

308309

309-
if sys.version_info >= (3, 10):
310-
from typing import TypeGuard
311-
else:
312-
from typing_extensions import TypeGuard
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:
313322

323+
def is_scalar(value: Any, include_0d: bool = True) -> bool:
324+
"""Whether to treat a value as a scalar.
314325
315-
def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]:
316-
"""Whether to treat a value as a scalar.
326+
Any non-iterable, string, or 0-D array
327+
"""
328+
return _is_scalar(value, include_0d)
317329

318-
Any non-iterable, string, or 0-D array
319-
"""
320-
return _is_scalar(value, include_0d)
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)
321339

322340

323341
def is_valid_numpy_dtype(dtype: Any) -> bool:

0 commit comments

Comments
 (0)