|
4 | 4 | import datetime |
5 | 5 | import importlib |
6 | 6 | from collections.abc import Iterable |
| 7 | +from types import ModuleType |
7 | 8 | from typing import Any |
8 | 9 |
|
9 | 10 | import numpy as np |
@@ -45,13 +46,20 @@ def module_available(module: str, minversion: str | None = None) -> bool: |
45 | 46 | except ImportError: |
46 | 47 | cftime = None |
47 | 48 |
|
| 49 | +cubed: ModuleType | None |
| 50 | +try: |
| 51 | + import cubed # type: ignore[no-redef] |
| 52 | +except ImportError: |
| 53 | + cubed = None |
48 | 54 |
|
| 55 | +dask: ModuleType | None |
49 | 56 | try: |
50 | 57 | import dask.array |
51 | 58 |
|
52 | | - dask_array_type = dask.array.Array |
| 59 | + dask_array_type = dask.array.Array # type: ignore[union-attr] |
53 | 60 | except ImportError: |
54 | | - dask_array_type = () # type: ignore[assignment, misc] |
| 61 | + dask = None |
| 62 | + dask_array_type = () |
55 | 63 |
|
56 | 64 |
|
57 | 65 | def asarray(data, xp=np): |
@@ -79,26 +87,19 @@ def is_chunked_array(x) -> bool: |
79 | 87 |
|
80 | 88 |
|
81 | 89 | def is_dask_collection(x): |
82 | | - try: |
83 | | - import dask |
84 | | - |
85 | | - return dask.is_dask_collection(x) |
86 | | - |
87 | | - except ImportError: |
| 90 | + if dask is None: |
88 | 91 | return False |
| 92 | + return dask.is_dask_collection(x) |
89 | 93 |
|
90 | 94 |
|
91 | 95 | def is_duck_dask_array(x): |
92 | 96 | return is_duck_array(x) and is_dask_collection(x) |
93 | 97 |
|
94 | 98 |
|
95 | 99 | def is_duck_cubed_array(x): |
96 | | - try: |
97 | | - import cubed |
98 | | - |
99 | | - return is_duck_array(x) and isinstance(x, cubed.Array) |
100 | | - except ImportError: |
| 100 | + if cubed is None: |
101 | 101 | return False |
| 102 | + return is_duck_array(x) and isinstance(x, cubed.Array) |
102 | 103 |
|
103 | 104 |
|
104 | 105 | class ReprObject: |
@@ -140,7 +141,7 @@ def is_scalar(value: Any, include_0d: bool = True) -> bool: |
140 | 141 | or isinstance(value, str | bytes | dict) |
141 | 142 | or not ( |
142 | 143 | isinstance(value, (Iterable,) + NON_NUMPY_SUPPORTED_ARRAY_TYPES) |
143 | | - or hasattr(value, "__array_function__") |
| 144 | + or hasattr(value, "__array_function__") # type: ignore[unreachable] |
144 | 145 | ) |
145 | 146 | ) |
146 | 147 |
|
@@ -182,13 +183,13 @@ def isnull(data: Any): |
182 | 183 | else: |
183 | 184 | # at this point, array should have dtype=object |
184 | 185 | if isinstance(data, (np.ndarray, dask_array_type)): # noqa |
185 | | - return pd.isnull(data) # type: ignore[arg-type] |
| 186 | + return pd.isnull(data) |
186 | 187 | else: |
187 | 188 | # Not reachable yet, but intended for use with other duck array |
188 | 189 | # types. For full consistency with pandas, we should accept None as |
189 | 190 | # a null value as well as NaN, but it isn't clear how to do this |
190 | 191 | # with duck typing. |
191 | | - return data != data |
| 192 | + return data != data # type: ignore[unreachable] |
192 | 193 |
|
193 | 194 |
|
194 | 195 | def datetime_to_numeric(array, offset=None, datetime_unit=None, dtype=float): |
|
0 commit comments