Skip to content

Commit 4394792

Browse files
TYP: explicit DTypeLike | None (pydata#10738)
* TYP: explicit `DTypeLike | None` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 98cfee5 commit 4394792

18 files changed

+40
-30
lines changed

xarray/backends/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,11 @@ class BackendArray(NdimSizeLenMixin, indexing.ExplicitlyIndexed):
309309
async def async_getitem(self, key: indexing.ExplicitIndexer) -> np.typing.ArrayLike:
310310
raise NotImplementedError("Backend does not support asynchronous loading")
311311

312-
def get_duck_array(self, dtype: np.typing.DTypeLike = None):
312+
def get_duck_array(self, dtype: np.typing.DTypeLike | None = None):
313313
key = indexing.BasicIndexer((slice(None),) * self.ndim)
314314
return self[key] # type: ignore[index]
315315

316-
async def async_get_duck_array(self, dtype: np.typing.DTypeLike = None):
316+
async def async_get_duck_array(self, dtype: np.typing.DTypeLike | None = None):
317317
key = indexing.BasicIndexer((slice(None),) * self.ndim)
318318
return await self.async_getitem(key)
319319

@@ -644,7 +644,7 @@ def _infer_dtype(array, name=None):
644644
)
645645

646646

647-
def _copy_with_dtype(data, dtype: np.typing.DTypeLike):
647+
def _copy_with_dtype(data, dtype: np.typing.DTypeLike | None):
648648
"""Create a copy of an array with the given dtype.
649649
650650
We use this instead of np.array() to ensure that custom object dtypes end

xarray/coding/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class _ElementwiseFunctionArray(indexing.ExplicitlyIndexedNDArrayMixin):
5353
Values are computed upon indexing or coercion to a NumPy array.
5454
"""
5555

56-
def __init__(self, array, func: Callable, dtype: np.typing.DTypeLike):
56+
def __init__(self, array, func: Callable, dtype: np.typing.DTypeLike | None):
5757
assert not is_chunked_array(array)
5858
self.array = indexing.as_indexable(array)
5959
self.func = func
@@ -86,7 +86,7 @@ def __repr__(self) -> str:
8686
return f"{type(self).__name__}({self.array!r}, func={self.func!r}, dtype={self.dtype!r})"
8787

8888

89-
def lazy_elemwise_func(array, func: Callable, dtype: np.typing.DTypeLike):
89+
def lazy_elemwise_func(array, func: Callable, dtype: np.typing.DTypeLike | None):
9090
"""Lazily apply an element-wise function to an array.
9191
Parameters
9292
----------

xarray/coding/variables.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _apply_mask(
122122
data: np.ndarray,
123123
encoded_fill_values: list,
124124
decoded_fill_value: Any,
125-
dtype: np.typing.DTypeLike,
125+
dtype: np.typing.DTypeLike | None,
126126
) -> np.ndarray:
127127
"""Mask all matching values in a NumPy arrays."""
128128
data = np.asarray(data, dtype=dtype)
@@ -426,7 +426,9 @@ def decode(self, variable: Variable, name: T_Name = None):
426426
return Variable(dims, data, attrs, encoding, fastpath=True)
427427

428428

429-
def _scale_offset_decoding(data, scale_factor, add_offset, dtype: np.typing.DTypeLike):
429+
def _scale_offset_decoding(
430+
data, scale_factor, add_offset, dtype: np.typing.DTypeLike | None
431+
):
430432
data = data.astype(dtype=dtype, copy=True)
431433
if scale_factor is not None:
432434
data *= scale_factor

xarray/core/accessor_dt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class TimeAccessor(Generic[T_DataArray]):
244244
def __init__(self, obj: T_DataArray) -> None:
245245
self._obj = obj
246246

247-
def _date_field(self, name: str, dtype: DTypeLike) -> T_DataArray:
247+
def _date_field(self, name: str, dtype: DTypeLike | None) -> T_DataArray:
248248
if dtype is None:
249249
dtype = self._obj.dtype
250250
result = _get_date_field(_index_or_data(self._obj), name, dtype)

xarray/core/accessor_str.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _apply_str_ufunc(
112112
*,
113113
func: Callable,
114114
obj: Any,
115-
dtype: DTypeLike = None,
115+
dtype: DTypeLike | None = None,
116116
output_core_dims: list | tuple = ((),),
117117
output_sizes: Mapping[Any, int] | None = None,
118118
func_args: tuple = (),
@@ -224,7 +224,7 @@ def _apply(
224224
self,
225225
*,
226226
func: Callable,
227-
dtype: DTypeLike = None,
227+
dtype: DTypeLike | None = None,
228228
output_core_dims: list | tuple = ((),),
229229
output_sizes: Mapping[Any, int] | None = None,
230230
func_args: tuple = (),

xarray/core/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def __complex__(self: Any) -> complex:
164164
return complex(self.values)
165165

166166
def __array__(
167-
self: Any, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None
167+
self: Any, dtype: DTypeLike | None = None, /, *, copy: bool | None = None
168168
) -> np.ndarray:
169169
if not copy:
170170
if np.lib.NumpyVersion(np.__version__) >= "2.0.0":
@@ -2073,12 +2073,12 @@ def get_chunksizes(
20732073
return Frozen(chunks)
20742074

20752075

2076-
def is_np_datetime_like(dtype: DTypeLike) -> bool:
2076+
def is_np_datetime_like(dtype: DTypeLike | None) -> bool:
20772077
"""Check if a dtype is a subclass of the numpy datetime types"""
20782078
return np.issubdtype(dtype, np.datetime64) or np.issubdtype(dtype, np.timedelta64)
20792079

20802080

2081-
def is_np_timedelta_like(dtype: DTypeLike) -> bool:
2081+
def is_np_timedelta_like(dtype: DTypeLike | None) -> bool:
20822082
"""Check whether dtype is of the timedelta64 dtype."""
20832083
return np.issubdtype(dtype, np.timedelta64)
20842084

xarray/core/datatree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ def __iter__(self) -> Iterator[str]:
818818
return itertools.chain(self._data_variables, self._children) # type: ignore[arg-type]
819819

820820
def __array__(
821-
self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None
821+
self, dtype: np.typing.DTypeLike | None = None, /, *, copy: bool | None = None
822822
) -> np.ndarray:
823823
raise TypeError(
824824
"cannot directly convert a DataTree into a "

xarray/core/dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def preprocess_types(t):
238238

239239

240240
def result_type(
241-
*arrays_and_dtypes: np.typing.ArrayLike | np.typing.DTypeLike,
241+
*arrays_and_dtypes: np.typing.ArrayLike | np.typing.DTypeLike | None,
242242
xp=None,
243243
) -> np.dtype:
244244
"""Like np.result_type, but with type promotion rules matching pandas.

xarray/core/extension_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def ndim(self) -> int:
160160
return 1
161161

162162
def __array__(
163-
self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None
163+
self, dtype: np.typing.DTypeLike | None = None, /, *, copy: bool | None = None
164164
) -> np.ndarray:
165165
if Version(np.__version__) >= Version("2.0.0"):
166166
return np.asarray(self.array, dtype=dtype, copy=copy)

xarray/core/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def data(self) -> np.ndarray:
211211
return np.arange(self.size, dtype=int)
212212

213213
def __array__(
214-
self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None
214+
self, dtype: np.typing.DTypeLike | None = None, /, *, copy: bool | None = None
215215
) -> np.ndarray:
216216
if copy is False:
217217
raise NotImplementedError(f"An array copy is necessary, got {copy = }.")

0 commit comments

Comments
 (0)