Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ repos:
types-pytz,
# Dependencies that are typed
numpy,
typing-extensions==3.10.0.0,
]
# run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
# - repo: https://github.com/asottile/pyupgrade
Expand Down
1 change: 0 additions & 1 deletion doc/getting-started-guide/installing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Required dependencies

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

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ classifiers =
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Topic :: Scientific/Engineering

[options]
Expand All @@ -78,7 +79,6 @@ install_requires =
numpy >= 1.17
pandas >= 1.0
setuptools >= 40.4 # For pkg_resources
typing-extensions >= 3.10 # Backported type hints

[options.extras_require]
io =
Expand Down
44 changes: 33 additions & 11 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import warnings
from enum import Enum
from typing import (
TYPE_CHECKING,
Any,
Callable,
Collection,
Expand All @@ -32,12 +33,6 @@
import numpy as np
import pandas as pd

if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from typing_extensions import TypeGuard


K = TypeVar("K")
V = TypeVar("V")
T = TypeVar("T")
Expand Down Expand Up @@ -297,11 +292,7 @@ def either_dict_or_kwargs(
return pos_kwargs


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
"""
def _is_scalar(value, include_0d):
from .variable import NON_NUMPY_SUPPORTED_ARRAY_TYPES

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


# 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)


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:
try:
np.dtype(dtype)
Expand Down