Skip to content

Commit 0203f8d

Browse files
TYP: Arraylike alias change follow up (#40398)
1 parent 548f44f commit 0203f8d

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

pandas/core/arrays/datetimelike.py

+10
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@
123123
from pandas.tseries import frequencies
124124

125125
if TYPE_CHECKING:
126+
from typing import Literal
127+
126128
from pandas.core.arrays import (
127129
DatetimeArray,
128130
TimedeltaArray,
@@ -458,6 +460,14 @@ def astype(self, dtype, copy=True):
458460
def view(self: DatetimeLikeArrayT) -> DatetimeLikeArrayT:
459461
...
460462

463+
@overload
464+
def view(self, dtype: Literal["M8[ns]"]) -> DatetimeArray:
465+
...
466+
467+
@overload
468+
def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray:
469+
...
470+
461471
@overload
462472
def view(self, dtype: Optional[Dtype] = ...) -> ArrayLike:
463473
...

pandas/core/arrays/period.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,7 @@ def fillna(self, value=None, method=None, limit=None) -> PeriodArray:
643643
if method is not None:
644644
# view as dt64 so we get treated as timelike in core.missing
645645
dta = self.view("M8[ns]")
646-
# error: Item "ndarray" of "Union[ExtensionArray, ndarray]" has no attribute
647-
# "fillna"
648-
result = dta.fillna( # type: ignore[union-attr]
649-
value=value, method=method, limit=limit
650-
)
646+
result = dta.fillna(value=value, method=method, limit=limit)
651647
return result.view(self.dtype)
652648
return super().fillna(value=value, method=method, limit=limit)
653649

pandas/core/arrays/sparse/dtype.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from pandas.core.dtypes.cast import astype_nansafe
2828
from pandas.core.dtypes.common import (
2929
is_bool_dtype,
30-
is_extension_array_dtype,
3130
is_object_dtype,
3231
is_scalar,
3332
is_string_dtype,
@@ -339,14 +338,10 @@ def update_dtype(self, dtype):
339338
dtype = pandas_dtype(dtype)
340339

341340
if not isinstance(dtype, cls):
342-
if is_extension_array_dtype(dtype):
341+
if not isinstance(dtype, np.dtype):
343342
raise TypeError("sparse arrays of extension dtypes not supported")
344343

345-
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
346-
# attribute "item"
347-
fill_value = astype_nansafe( # type: ignore[union-attr]
348-
np.array(self.fill_value), dtype
349-
).item()
344+
fill_value = astype_nansafe(np.array(self.fill_value), dtype).item()
350345
dtype = cls(dtype, fill_value=fill_value)
351346

352347
return dtype

pandas/core/dtypes/cast.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Type,
2626
Union,
2727
cast,
28+
overload,
2829
)
2930
import warnings
3031

@@ -107,6 +108,8 @@
107108
)
108109

109110
if TYPE_CHECKING:
111+
from typing import Literal
112+
110113
from pandas import Series
111114
from pandas.core.arrays import (
112115
DatetimeArray,
@@ -1164,6 +1167,20 @@ def astype_td64_unit_conversion(
11641167
return result
11651168

11661169

1170+
@overload
1171+
def astype_nansafe(
1172+
arr: np.ndarray, dtype: np.dtype, copy: bool = ..., skipna: bool = ...
1173+
) -> np.ndarray:
1174+
...
1175+
1176+
1177+
@overload
1178+
def astype_nansafe(
1179+
arr: np.ndarray, dtype: ExtensionDtype, copy: bool = ..., skipna: bool = ...
1180+
) -> ExtensionArray:
1181+
...
1182+
1183+
11671184
def astype_nansafe(
11681185
arr: np.ndarray, dtype: DtypeObj, copy: bool = True, skipna: bool = False
11691186
) -> ArrayLike:
@@ -1190,14 +1207,10 @@ def astype_nansafe(
11901207
flags = arr.flags
11911208
flat = arr.ravel("K")
11921209
result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna)
1193-
order = "F" if flags.f_contiguous else "C"
1210+
order: Literal["C", "F"] = "F" if flags.f_contiguous else "C"
11941211
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
11951212
# attribute "reshape"
1196-
# error: No overload variant of "reshape" of "_ArrayOrScalarCommon" matches
1197-
# argument types "Tuple[int, ...]", "str"
1198-
return result.reshape( # type: ignore[union-attr,call-overload]
1199-
arr.shape, order=order
1200-
)
1213+
return result.reshape(arr.shape, order=order) # type: ignore[union-attr]
12011214

12021215
# We get here with 0-dim from sparse
12031216
arr = np.atleast_1d(arr)

0 commit comments

Comments
 (0)