Skip to content

Commit 9664284

Browse files
authored
TYP: fix ignores (#40452)
1 parent cb130fd commit 9664284

File tree

12 files changed

+109
-204
lines changed

12 files changed

+109
-204
lines changed

pandas/_libs/writers.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def write_csv_rows(
3434
data_index : ndarray
3535
nlevels : int
3636
cols : ndarray
37-
writer : object
37+
writer : _csv.writer
3838
"""
3939
# In crude testing, N>100 yields little marginal improvement
4040
cdef:

pandas/core/array_algos/quantile.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,10 @@ def quantile_ea_compat(
142142
mask = np.asarray(values.isna())
143143
mask = np.atleast_2d(mask)
144144

145-
# error: Incompatible types in assignment (expression has type "ndarray", variable
146-
# has type "ExtensionArray")
147-
values, fill_value = values._values_for_factorize() # type: ignore[assignment]
148-
# error: No overload variant of "atleast_2d" matches argument type "ExtensionArray"
149-
values = np.atleast_2d(values) # type: ignore[call-overload]
150-
151-
# error: Argument 1 to "quantile_with_mask" has incompatible type "ExtensionArray";
152-
# expected "ndarray"
153-
result = quantile_with_mask(
154-
values, mask, fill_value, qs, interpolation, axis # type: ignore[arg-type]
155-
)
145+
arr, fill_value = values._values_for_factorize()
146+
arr = np.atleast_2d(arr)
147+
148+
result = quantile_with_mask(arr, mask, fill_value, qs, interpolation, axis)
156149

157150
if not is_sparse(orig.dtype):
158151
# shape[0] should be 1 as long as EAs are 1D

pandas/core/array_algos/replace.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ def re_replacer(s):
152152
f = np.vectorize(re_replacer, otypes=[values.dtype])
153153

154154
if mask is None:
155-
# error: Invalid index type "slice" for "ExtensionArray"; expected type
156-
# "Union[int, ndarray]"
157-
values[:] = f(values) # type: ignore[index]
155+
values[:] = f(values)
158156
else:
159157
values[mask] = f(values[mask])

pandas/core/array_algos/take.py

-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ def take_1d(
177177
178178
Note: similarly to `take_nd`, this function assumes that the indexer is
179179
a valid(ated) indexer with no out of bound indices.
180-
181-
TODO(ArrayManager): mainly useful for ArrayManager, otherwise can potentially
182-
be removed again if we don't end up with ArrayManager.
183180
"""
184181
if not isinstance(arr, np.ndarray):
185182
# ExtensionArray -> dispatch to their method

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def __getitem__(
326326
"""
327327
raise AbstractMethodError(self)
328328

329-
def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
329+
def __setitem__(self, key: Union[int, slice, np.ndarray], value: Any) -> None:
330330
"""
331331
Set one or more values inplace.
332332

pandas/core/arrays/datetimes.py

+19
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
tzinfo,
88
)
99
from typing import (
10+
TYPE_CHECKING,
1011
Optional,
1112
Union,
1213
cast,
14+
overload,
1315
)
1416
import warnings
1517

@@ -79,6 +81,9 @@
7981
Tick,
8082
)
8183

84+
if TYPE_CHECKING:
85+
from typing import Literal
86+
8287
_midnight = time(0, 0)
8388

8489

@@ -1909,6 +1914,20 @@ def std(
19091914
# Constructor Helpers
19101915

19111916

1917+
@overload
1918+
def sequence_to_datetimes(
1919+
data, allow_object: Literal[False] = ..., require_iso8601: bool = ...
1920+
) -> DatetimeArray:
1921+
...
1922+
1923+
1924+
@overload
1925+
def sequence_to_datetimes(
1926+
data, allow_object: Literal[True] = ..., require_iso8601: bool = ...
1927+
) -> Union[np.ndarray, DatetimeArray]:
1928+
...
1929+
1930+
19121931
def sequence_to_datetimes(
19131932
data, allow_object: bool = False, require_iso8601: bool = False
19141933
) -> Union[np.ndarray, DatetimeArray]:

pandas/core/arrays/string_arrow.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Tuple,
1010
Type,
1111
Union,
12+
cast,
1213
)
1314

1415
import numpy as np
@@ -485,7 +486,7 @@ def _cmp_method(self, other, op):
485486
# TODO(ARROW-9429): Add a .to_numpy() to ChunkedArray
486487
return BooleanArray._from_sequence(result.to_pandas().values)
487488

488-
def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
489+
def __setitem__(self, key: Union[int, slice, np.ndarray], value: Any) -> None:
489490
"""Set one or more values inplace.
490491
491492
Parameters
@@ -509,6 +510,8 @@ def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
509510
key = check_array_indexer(self, key)
510511

511512
if is_integer(key):
513+
key = cast(int, key)
514+
512515
if not is_scalar(value):
513516
raise ValueError("Must pass scalars with scalar indexer")
514517
elif isna(value):
@@ -518,8 +521,7 @@ def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
518521

519522
# Slice data and insert in-between
520523
new_data = [
521-
# error: Slice index must be an integer or None
522-
*self._data[0:key].chunks, # type: ignore[misc]
524+
*self._data[0:key].chunks,
523525
pa.array([value], type=pa.string()),
524526
*self._data[(key + 1) :].chunks,
525527
]
@@ -530,11 +532,11 @@ def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
530532
# This is probably extremely slow.
531533

532534
# Convert all possible input key types to an array of integers
533-
if is_bool_dtype(key):
535+
if isinstance(key, slice):
536+
key_array = np.array(range(len(self))[key])
537+
elif is_bool_dtype(key):
534538
# TODO(ARROW-9430): Directly support setitem(booleans)
535539
key_array = np.argwhere(key).flatten()
536-
elif isinstance(key, slice):
537-
key_array = np.array(range(len(self))[key])
538540
else:
539541
# TODO(ARROW-9431): Directly support setitem(integers)
540542
key_array = np.asanyarray(key)

pandas/core/dtypes/cast.py

+34-74
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
)
4545
from pandas._libs.tslibs.timedeltas import array_to_timedelta64
4646
from pandas._typing import (
47-
AnyArrayLike,
4847
ArrayLike,
4948
Dtype,
5049
DtypeObj,
@@ -407,27 +406,16 @@ def maybe_cast_result(
407406

408407
assert not is_scalar(result)
409408

410-
if (
411-
is_extension_array_dtype(dtype)
412-
and not is_categorical_dtype(dtype)
413-
and dtype.kind != "M"
414-
):
415-
# We have to special case categorical so as not to upcast
416-
# things like counts back to categorical
417-
418-
# error: Item "dtype[Any]" of "Union[dtype[Any], ExtensionDtype]" has no
419-
# attribute "construct_array_type"
420-
cls = dtype.construct_array_type() # type: ignore[union-attr]
421-
# error: Argument "dtype" to "maybe_cast_to_extension_array" has incompatible
422-
# type "Union[dtype[Any], ExtensionDtype]"; expected "Optional[ExtensionDtype]"
423-
result = maybe_cast_to_extension_array(
424-
cls, result, dtype=dtype # type: ignore[arg-type]
425-
)
409+
if isinstance(dtype, ExtensionDtype):
410+
if not is_categorical_dtype(dtype) and dtype.kind != "M":
411+
# We have to special case categorical so as not to upcast
412+
# things like counts back to categorical
426413

427-
elif numeric_only and is_numeric_dtype(dtype) or not numeric_only:
428-
# error: Argument 2 to "maybe_downcast_to_dtype" has incompatible type
429-
# "Union[dtype[Any], ExtensionDtype]"; expected "Union[str, dtype[Any]]"
430-
result = maybe_downcast_to_dtype(result, dtype) # type: ignore[arg-type]
414+
cls = dtype.construct_array_type()
415+
result = maybe_cast_to_extension_array(cls, result, dtype=dtype)
416+
417+
elif (numeric_only and is_numeric_dtype(dtype)) or not numeric_only:
418+
result = maybe_downcast_to_dtype(result, dtype)
431419

432420
return result
433421

@@ -549,17 +537,23 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray) -> np.ndarray:
549537
new_dtype = ensure_dtype_can_hold_na(result.dtype)
550538

551539
if new_dtype != result.dtype:
552-
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has incompatible
553-
# type "Union[dtype[Any], ExtensionDtype]"; expected "Union[dtype[Any],
554-
# None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any,
555-
# Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
556-
result = result.astype(new_dtype, copy=True) # type: ignore[arg-type]
540+
result = result.astype(new_dtype, copy=True)
557541

558542
np.place(result, mask, np.nan)
559543

560544
return result
561545

562546

547+
@overload
548+
def ensure_dtype_can_hold_na(dtype: np.dtype) -> np.dtype:
549+
...
550+
551+
552+
@overload
553+
def ensure_dtype_can_hold_na(dtype: ExtensionDtype) -> ExtensionDtype:
554+
...
555+
556+
563557
def ensure_dtype_can_hold_na(dtype: DtypeObj) -> DtypeObj:
564558
"""
565559
If we have a dtype that cannot hold NA values, find the best match that can.
@@ -636,9 +630,7 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan):
636630

637631
kinds = ["i", "u", "f", "c", "m", "M"]
638632
if is_valid_na_for_dtype(fill_value, dtype) and dtype.kind in kinds:
639-
# error: Incompatible types in assignment (expression has type
640-
# "Union[dtype[Any], ExtensionDtype]", variable has type "dtype[Any]")
641-
dtype = ensure_dtype_can_hold_na(dtype) # type: ignore[assignment]
633+
dtype = ensure_dtype_can_hold_na(dtype)
642634
fv = na_value_for_dtype(dtype)
643635
return dtype, fv
644636

@@ -1471,7 +1463,7 @@ def soft_convert_objects(
14711463

14721464

14731465
def convert_dtypes(
1474-
input_array: AnyArrayLike,
1466+
input_array: ArrayLike,
14751467
convert_string: bool = True,
14761468
convert_integer: bool = True,
14771469
convert_boolean: bool = True,
@@ -1483,7 +1475,7 @@ def convert_dtypes(
14831475
14841476
Parameters
14851477
----------
1486-
input_array : ExtensionArray, Index, Series or np.ndarray
1478+
input_array : ExtensionArray or np.ndarray
14871479
convert_string : bool, default True
14881480
Whether object dtypes should be converted to ``StringDtype()``.
14891481
convert_integer : bool, default True
@@ -1707,15 +1699,10 @@ def maybe_cast_to_datetime(
17071699
# GH 25843: Remove tz information since the dtype
17081700
# didn't specify one
17091701

1710-
# error: Item "ndarray" of "Union[ndarray, DatetimeArray]"
1711-
# has no attribute "tz"
1712-
if dta.tz is not None: # type: ignore[union-attr]
1702+
if dta.tz is not None:
17131703
# equiv: dta.view(dtype)
17141704
# Note: NOT equivalent to dta.astype(dtype)
1715-
1716-
# error: Item "ndarray" of "Union[ndarray,
1717-
# DatetimeArray]" has no attribute "tz_localize"
1718-
dta = dta.tz_localize(None) # type: ignore[union-attr]
1705+
dta = dta.tz_localize(None)
17191706
value = dta
17201707
elif is_datetime64tz:
17211708
dtype = cast(DatetimeTZDtype, dtype)
@@ -1725,38 +1712,19 @@ def maybe_cast_to_datetime(
17251712
# be localized to the timezone.
17261713
is_dt_string = is_string_dtype(value.dtype)
17271714
dta = sequence_to_datetimes(value, allow_object=False)
1728-
# error: Item "ndarray" of "Union[ndarray, DatetimeArray]"
1729-
# has no attribute "tz"
1730-
if dta.tz is not None: # type: ignore[union-attr]
1731-
# error: Argument 1 to "astype" of
1732-
# "_ArrayOrScalarCommon" has incompatible type
1733-
# "Union[dtype[Any], ExtensionDtype, None]"; expected
1734-
# "Union[dtype[Any], None, type, _SupportsDType, str,
1735-
# Union[Tuple[Any, int], Tuple[Any, Union[int,
1736-
# Sequence[int]]], List[Any], _DTypeDict, Tuple[Any,
1737-
# Any]]]"
1738-
value = dta.astype(
1739-
dtype, copy=False # type: ignore[arg-type]
1740-
)
1715+
if dta.tz is not None:
1716+
value = dta.astype(dtype, copy=False)
17411717
elif is_dt_string:
17421718
# Strings here are naive, so directly localize
17431719
# equiv: dta.astype(dtype) # though deprecated
17441720

1745-
# error: Item "ndarray" of "Union[ndarray,
1746-
# DatetimeArray]" has no attribute "tz_localize"
1747-
value = dta.tz_localize( # type: ignore[union-attr]
1748-
dtype.tz
1749-
)
1721+
value = dta.tz_localize(dtype.tz)
17501722
else:
17511723
# Numeric values are UTC at this point,
17521724
# so localize and convert
17531725
# equiv: Series(dta).astype(dtype) # though deprecated
17541726

1755-
# error: Item "ndarray" of "Union[ndarray,
1756-
# DatetimeArray]" has no attribute "tz_localize"
1757-
value = dta.tz_localize( # type: ignore[union-attr]
1758-
"UTC"
1759-
).tz_convert(dtype.tz)
1727+
value = dta.tz_localize("UTC").tz_convert(dtype.tz)
17601728
elif is_timedelta64:
17611729
# if successful, we get a ndarray[td64ns]
17621730
value, _ = sequence_to_td64ns(value)
@@ -1789,14 +1757,12 @@ def maybe_cast_to_datetime(
17891757
elif value.dtype == object:
17901758
value = maybe_infer_to_datetimelike(value)
17911759

1792-
elif not isinstance(value, ABCExtensionArray):
1760+
elif isinstance(value, list):
17931761
# only do this if we have an array and the dtype of the array is not
17941762
# setup already we are not an integer/object, so don't bother with this
17951763
# conversion
17961764

1797-
# error: Argument 1 to "maybe_infer_to_datetimelike" has incompatible type
1798-
# "Union[ExtensionArray, List[Any]]"; expected "Union[ndarray, List[Any]]"
1799-
value = maybe_infer_to_datetimelike(value) # type: ignore[arg-type]
1765+
value = maybe_infer_to_datetimelike(value)
18001766

18011767
return value
18021768

@@ -1974,10 +1940,8 @@ def construct_1d_arraylike_from_scalar(
19741940
except OutOfBoundsDatetime:
19751941
dtype = np.dtype(object)
19761942

1977-
if is_extension_array_dtype(dtype):
1978-
# error: Item "dtype" of "Union[dtype, ExtensionDtype]" has no
1979-
# attribute "construct_array_type"
1980-
cls = dtype.construct_array_type() # type: ignore[union-attr]
1943+
if isinstance(dtype, ExtensionDtype):
1944+
cls = dtype.construct_array_type()
19811945
subarr = cls._from_sequence([value] * length, dtype=dtype)
19821946

19831947
else:
@@ -1994,11 +1958,7 @@ def construct_1d_arraylike_from_scalar(
19941958
elif dtype.kind in ["M", "m"]:
19951959
value = maybe_unbox_datetimelike(value, dtype)
19961960

1997-
# error: Argument "dtype" to "empty" has incompatible type
1998-
# "Union[dtype, ExtensionDtype]"; expected "Union[dtype, None, type,
1999-
# _SupportsDtype, str, Tuple[Any, int], Tuple[Any, Union[int,
2000-
# Sequence[int]]], List[Any], _DtypeDict, Tuple[Any, Any]]"
2001-
subarr = np.empty(length, dtype=dtype) # type: ignore[arg-type]
1961+
subarr = np.empty(length, dtype=dtype)
20021962
subarr.fill(value)
20031963

20041964
return subarr

0 commit comments

Comments
 (0)