|
10 | 10 | import warnings
|
11 | 11 | from enum import Enum
|
12 | 12 | from typing import (
|
| 13 | + TYPE_CHECKING, |
13 | 14 | Any,
|
14 | 15 | Callable,
|
15 | 16 | Collection,
|
@@ -306,18 +307,35 @@ def _is_scalar(value, include_0d):
|
306 | 307 | )
|
307 | 308 |
|
308 | 309 |
|
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: |
313 | 322 |
|
| 323 | + def is_scalar(value: Any, include_0d: bool = True) -> bool: |
| 324 | + """Whether to treat a value as a scalar. |
314 | 325 |
|
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) |
317 | 329 |
|
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) |
321 | 339 |
|
322 | 340 |
|
323 | 341 | def is_valid_numpy_dtype(dtype: Any) -> bool:
|
|
0 commit comments