Skip to content

Commit 8c9b84d

Browse files
authored
TYP: Fix typing errors on main (#47469)
1 parent cc41458 commit 8c9b84d

File tree

22 files changed

+137
-85
lines changed

22 files changed

+137
-85
lines changed

pandas/core/algorithms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,13 @@ def checked_add_with_arr(
10641064
elif arr_mask is not None:
10651065
not_nan = np.logical_not(arr_mask)
10661066
elif b_mask is not None:
1067-
not_nan = np.logical_not(b2_mask)
1067+
# error: Argument 1 to "__call__" of "_UFunc_Nin1_Nout1" has
1068+
# incompatible type "Optional[ndarray[Any, dtype[bool_]]]";
1069+
# expected "Union[_SupportsArray[dtype[Any]], _NestedSequence
1070+
# [_SupportsArray[dtype[Any]]], bool, int, float, complex, str
1071+
# , bytes, _NestedSequence[Union[bool, int, float, complex, str
1072+
# , bytes]]]"
1073+
not_nan = np.logical_not(b2_mask) # type: ignore[arg-type]
10681074
else:
10691075
not_nan = np.empty(arr.shape, dtype=bool)
10701076
not_nan.fill(True)

pandas/core/array_algos/quantile.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ def _nanpercentile_1d(
143143
return np.percentile(
144144
values,
145145
qs,
146-
**{np_percentile_argname: interpolation},
146+
# error: No overload variant of "percentile" matches argument
147+
# types "ndarray[Any, Any]", "ndarray[Any, dtype[floating[_64Bit]]]"
148+
# , "Dict[str, str]" [call-overload]
149+
**{np_percentile_argname: interpolation}, # type: ignore[call-overload]
147150
)
148151

149152

@@ -212,5 +215,8 @@ def _nanpercentile(
212215
values,
213216
qs,
214217
axis=1,
215-
**{np_percentile_argname: interpolation},
218+
# error: No overload variant of "percentile" matches argument types
219+
# "ndarray[Any, Any]", "ndarray[Any, dtype[floating[_64Bit]]]",
220+
# "int", "Dict[str, str]" [call-overload]
221+
**{np_percentile_argname: interpolation}, # type: ignore[call-overload]
216222
)

pandas/core/arraylike.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,8 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
265265
return result
266266

267267
# Determine if we should defer.
268-
# error: "Type[ndarray[Any, Any]]" has no attribute "__array_ufunc__"
269268
no_defer = (
270-
np.ndarray.__array_ufunc__, # type: ignore[attr-defined]
269+
np.ndarray.__array_ufunc__,
271270
cls.__array_ufunc__,
272271
)
273272

pandas/core/arrays/arrow/array.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,15 @@ def _indexing_key_to_indices(
496496
if isinstance(key, slice):
497497
indices = np.arange(n)[key]
498498
elif is_integer(key):
499-
indices = np.arange(n)[[key]]
499+
# error: Invalid index type "List[Union[int, ndarray[Any, Any]]]"
500+
# for "ndarray[Any, dtype[signedinteger[Any]]]"; expected type
501+
# "Union[SupportsIndex, _SupportsArray[dtype[Union[bool_,
502+
# integer[Any]]]], _NestedSequence[_SupportsArray[dtype[Union
503+
# [bool_, integer[Any]]]]], _NestedSequence[Union[bool, int]]
504+
# , Tuple[Union[SupportsIndex, _SupportsArray[dtype[Union[bool_
505+
# , integer[Any]]]], _NestedSequence[_SupportsArray[dtype[Union
506+
# [bool_, integer[Any]]]]], _NestedSequence[Union[bool, int]]], ...]]"
507+
indices = np.arange(n)[[key]] # type: ignore[index]
500508
elif is_bool_dtype(key):
501509
key = np.asarray(key)
502510
if len(key) != n:

pandas/core/arrays/datetimes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,7 @@ def _generate_range(
487487
np.linspace(0, end.value - start.value, periods, dtype="int64")
488488
+ start.value
489489
)
490-
# error: Non-overlapping equality check
491-
# (left operand type: "dtype[signedinteger[Any]]",
492-
# right operand type: "Literal['i8']")
493-
if i8values.dtype != "i8": # type: ignore[comparison-overlap]
490+
if i8values.dtype != "i8":
494491
# 2022-01-09 I (brock) am not sure if it is possible for this
495492
# to overflow and cast to e.g. f8, but if it does we need to cast
496493
i8values = i8values.astype("i8")

pandas/core/arrays/interval.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -687,21 +687,7 @@ def __getitem__(
687687
if is_scalar(left) and isna(left):
688688
return self._fill_value
689689
return Interval(left, right, inclusive=self.inclusive)
690-
# error: Argument 1 to "ndim" has incompatible type
691-
# "Union[ndarray[Any, Any], ExtensionArray]"; expected
692-
# "Union[Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]],
693-
# Union[Union[_SupportsArray[dtype[Any]],
694-
# Sequence[_SupportsArray[dtype[Any]]],
695-
# Sequence[Sequence[_SupportsArray[dtype[Any]]]],
696-
# Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]],
697-
# Sequence[Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]]]],
698-
# Union[bool, int, float, complex, str, bytes,
699-
# Sequence[Union[bool, int, float, complex, str, bytes]],
700-
# Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]],
701-
# Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]],
702-
# Sequence[Sequence[Sequence[Sequence[Union[bool, int, float,
703-
# complex, str, bytes]]]]]]]]"
704-
if np.ndim(left) > 1: # type: ignore[arg-type]
690+
if np.ndim(left) > 1:
705691
# GH#30588 multi-dimensional indexer disallowed
706692
raise ValueError("multi-dimensional indexing not allowed")
707693
return self._shallow_copy(left, right)
@@ -1679,7 +1665,14 @@ def isin(self, values) -> np.ndarray:
16791665
# complex128 ndarray is much more performant.
16801666
left = self._combined.view("complex128")
16811667
right = values._combined.view("complex128")
1682-
return np.in1d(left, right)
1668+
# error: Argument 1 to "in1d" has incompatible type
1669+
# "Union[ExtensionArray, ndarray[Any, Any],
1670+
# ndarray[Any, dtype[Any]]]"; expected
1671+
# "Union[_SupportsArray[dtype[Any]],
1672+
# _NestedSequence[_SupportsArray[dtype[Any]]], bool,
1673+
# int, float, complex, str, bytes, _NestedSequence[
1674+
# Union[bool, int, float, complex, str, bytes]]]"
1675+
return np.in1d(left, right) # type: ignore[arg-type]
16831676

16841677
elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
16851678
values.left.dtype

pandas/core/arrays/masked.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,7 @@ def __init__(
110110
self, values: np.ndarray, mask: npt.NDArray[np.bool_], copy: bool = False
111111
) -> None:
112112
# values is supposed to already be validated in the subclass
113-
if not (
114-
isinstance(mask, np.ndarray)
115-
and
116-
# error: Non-overlapping equality check
117-
# (left operand type: "dtype[bool_]", right operand type: "Type[bool_]")
118-
mask.dtype == np.bool_ # type: ignore[comparison-overlap]
119-
):
113+
if not (isinstance(mask, np.ndarray) and mask.dtype == np.bool_):
120114
raise TypeError(
121115
"mask should be boolean numpy array. Use "
122116
"the 'pd.array' function instead"
@@ -1157,7 +1151,12 @@ def any(self, *, skipna: bool = True, **kwargs):
11571151
nv.validate_any((), kwargs)
11581152

11591153
values = self._data.copy()
1160-
np.putmask(values, self._mask, self._falsey_value)
1154+
# error: Argument 3 to "putmask" has incompatible type "object";
1155+
# expected "Union[_SupportsArray[dtype[Any]],
1156+
# _NestedSequence[_SupportsArray[dtype[Any]]],
1157+
# bool, int, float, complex, str, bytes,
1158+
# _NestedSequence[Union[bool, int, float, complex, str, bytes]]]"
1159+
np.putmask(values, self._mask, self._falsey_value) # type: ignore[arg-type]
11611160
result = values.any()
11621161
if skipna:
11631162
return result
@@ -1233,7 +1232,12 @@ def all(self, *, skipna: bool = True, **kwargs):
12331232
nv.validate_all((), kwargs)
12341233

12351234
values = self._data.copy()
1236-
np.putmask(values, self._mask, self._truthy_value)
1235+
# error: Argument 3 to "putmask" has incompatible type "object";
1236+
# expected "Union[_SupportsArray[dtype[Any]],
1237+
# _NestedSequence[_SupportsArray[dtype[Any]]],
1238+
# bool, int, float, complex, str, bytes,
1239+
# _NestedSequence[Union[bool, int, float, complex, str, bytes]]]"
1240+
np.putmask(values, self._mask, self._truthy_value) # type: ignore[arg-type]
12371241
result = values.all()
12381242

12391243
if skipna:

pandas/core/arrays/sparse/array.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,16 @@ def __getitem__(
944944
if is_integer(key):
945945
return self._get_val_at(key)
946946
elif isinstance(key, tuple):
947-
data_slice = self.to_dense()[key]
947+
# error: Invalid index type "Tuple[Union[int, ellipsis], ...]"
948+
# for "ndarray[Any, Any]"; expected type
949+
# "Union[SupportsIndex, _SupportsArray[dtype[Union[bool_,
950+
# integer[Any]]]], _NestedSequence[_SupportsArray[dtype[
951+
# Union[bool_, integer[Any]]]]], _NestedSequence[Union[
952+
# bool, int]], Tuple[Union[SupportsIndex, _SupportsArray[
953+
# dtype[Union[bool_, integer[Any]]]], _NestedSequence[
954+
# _SupportsArray[dtype[Union[bool_, integer[Any]]]]],
955+
# _NestedSequence[Union[bool, int]]], ...]]"
956+
data_slice = self.to_dense()[key] # type: ignore[index]
948957
elif isinstance(key, slice):
949958

950959
# Avoid densifying when handling contiguous slices
@@ -1184,7 +1193,10 @@ def _concat_same_type(
11841193

11851194
data = np.concatenate(values)
11861195
indices_arr = np.concatenate(indices)
1187-
sp_index = IntIndex(length, indices_arr)
1196+
# error: Argument 2 to "IntIndex" has incompatible type
1197+
# "ndarray[Any, dtype[signedinteger[_32Bit]]]";
1198+
# expected "Sequence[int]"
1199+
sp_index = IntIndex(length, indices_arr) # type: ignore[arg-type]
11881200

11891201
else:
11901202
# when concatenating block indices, we don't claim that you'll
@@ -1374,7 +1386,8 @@ def __setstate__(self, state):
13741386
if isinstance(state, tuple):
13751387
# Compat for pandas < 0.24.0
13761388
nd_state, (fill_value, sp_index) = state
1377-
sparse_values = np.array([])
1389+
# error: Need type annotation for "sparse_values"
1390+
sparse_values = np.array([]) # type: ignore[var-annotated]
13781391
sparse_values.__setstate__(nd_state)
13791392

13801393
self._sparse_values = sparse_values

pandas/core/dtypes/astype.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ def astype_nansafe(
113113
).reshape(shape)
114114

115115
elif is_datetime64_dtype(arr.dtype):
116-
# error: Non-overlapping equality check (left
117-
# operand type: "dtype[Any]", right operand type: "Type[signedinteger[Any]]")
118-
if dtype == np.int64: # type: ignore[comparison-overlap]
116+
if dtype == np.int64:
119117
if isna(arr).any():
120118
raise ValueError("Cannot convert NaT values to integer")
121119
return arr.view(dtype)
@@ -127,9 +125,7 @@ def astype_nansafe(
127125
raise TypeError(f"cannot astype a datetimelike from [{arr.dtype}] to [{dtype}]")
128126

129127
elif is_timedelta64_dtype(arr.dtype):
130-
# error: Non-overlapping equality check (left
131-
# operand type: "dtype[Any]", right operand type: "Type[signedinteger[Any]]")
132-
if dtype == np.int64: # type: ignore[comparison-overlap]
128+
if dtype == np.int64:
133129
if isna(arr).any():
134130
raise ValueError("Cannot convert NaT values to integer")
135131
return arr.view(dtype)

pandas/core/dtypes/common.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,7 @@ def is_string_or_object_np_dtype(dtype: np.dtype) -> bool:
534534
"""
535535
Faster alternative to is_string_dtype, assumes we have a np.dtype object.
536536
"""
537-
# error: Non-overlapping equality check (left operand type:
538-
# "dtype[Any]", right operand type: "Type[object]")
539-
return dtype == object or dtype.kind in "SU" # type: ignore[comparison-overlap]
537+
return dtype == object or dtype.kind in "SU"
540538

541539

542540
def is_string_dtype(arr_or_dtype) -> bool:

0 commit comments

Comments
 (0)