Skip to content

Commit 4388b5e

Browse files
committed
Use explicit cast to address type errors
1 parent 799941c commit 4388b5e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

xarray/core/dtypes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from xarray.compat import array_api_compat, npcompat
1111
from xarray.compat.npcompat import HAS_STRING_DTYPE
1212
from xarray.core import utils
13+
from xarray.core.types import PDDatetimeUnitOptions
1314

1415
if TYPE_CHECKING:
1516
from typing import Any
@@ -89,7 +90,10 @@ def maybe_promote(dtype: T_dtype) -> tuple[T_dtype, Any]:
8990
# np.timedelta64 is a subclass of np.integer
9091
# Check np.timedelta64 before np.integer
9192
unit, _ = np.datetime_data(dtype)
92-
fill_value = np.timedelta64("NaT", unit) # type: ignore[call-overload]
93+
# np.datetime_data returns a generic str so we need to cast it to
94+
# a valid time unit for mypy purposes.
95+
unit = cast(PDDatetimeUnitOptions, unit)
96+
fill_value = np.timedelta64("NaT", unit)
9397
dtype_ = dtype
9498
elif isdtype(dtype, "integral"):
9599
dtype_ = np.float32 if dtype.itemsize <= 2 else np.float64
@@ -99,8 +103,11 @@ def maybe_promote(dtype: T_dtype) -> tuple[T_dtype, Any]:
99103
fill_value = np.nan + np.nan * 1j
100104
elif np.issubdtype(dtype, np.datetime64):
101105
unit, _ = np.datetime_data(dtype)
106+
# np.datetime_data returns a generic str so we need to cast it to
107+
# a valid time unit for mypy purposes.
108+
unit = cast(PDDatetimeUnitOptions, unit)
102109
dtype_ = dtype
103-
fill_value = np.datetime64("NaT", unit) # type: ignore[call-overload]
110+
fill_value = np.datetime64("NaT", unit)
104111
else:
105112
dtype_ = object
106113
fill_value = np.nan

0 commit comments

Comments
 (0)