Skip to content

Commit 3368969

Browse files
authored
TYP: Fix typing errors caused by new numpy (#48850)
1 parent d719840 commit 3368969

File tree

11 files changed

+49
-31
lines changed

11 files changed

+49
-31
lines changed

pandas/_typing.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,19 @@
6565

6666
from pandas.io.formats.format import EngFormatter
6767

68+
ScalarLike_co = Union[
69+
int,
70+
float,
71+
complex,
72+
str,
73+
bytes,
74+
np.generic,
75+
]
76+
6877
# numpy compatible types
69-
NumpyValueArrayLike = Union[npt._ScalarLike_co, npt.ArrayLike]
70-
NumpySorter = Optional[npt._ArrayLikeInt_co]
78+
NumpyValueArrayLike = Union[ScalarLike_co, npt.ArrayLike]
79+
# Name "npt._ArrayLikeInt_co" is not defined [name-defined]
80+
NumpySorter = Optional[npt._ArrayLikeInt_co] # type: ignore[name-defined]
7181

7282
else:
7383
npt: Any = None

pandas/core/arrays/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,9 @@ def size(self) -> int:
542542
"""
543543
The number of elements in the array.
544544
"""
545-
return np.prod(self.shape)
545+
# error: Incompatible return value type (got "signedinteger[_64Bit]",
546+
# expected "int") [return-value]
547+
return np.prod(self.shape) # type: ignore[return-value]
546548

547549
@property
548550
def ndim(self) -> int:

pandas/core/arrays/sparse/array.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1426,8 +1426,7 @@ def __setstate__(self, state) -> None:
14261426
if isinstance(state, tuple):
14271427
# Compat for pandas < 0.24.0
14281428
nd_state, (fill_value, sp_index) = state
1429-
# error: Need type annotation for "sparse_values"
1430-
sparse_values = np.array([]) # type: ignore[var-annotated]
1429+
sparse_values = np.array([])
14311430
sparse_values.__setstate__(nd_state)
14321431

14331432
self._sparse_values = sparse_values

pandas/core/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
DropKeep,
8585
NumpySorter,
8686
NumpyValueArrayLike,
87+
ScalarLike_co,
8788
)
8889

8990
from pandas import (
@@ -1275,7 +1276,7 @@ def factorize(
12751276
# return types [misc]
12761277
def searchsorted( # type: ignore[misc]
12771278
self,
1278-
value: npt._ScalarLike_co,
1279+
value: ScalarLike_co,
12791280
side: Literal["left", "right"] = ...,
12801281
sorter: NumpySorter = ...,
12811282
) -> np.intp:

pandas/core/dtypes/missing.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,5 @@ def isna_all(arr: ArrayLike) -> bool:
775775
)
776776

777777
return all(
778-
# error: Argument 1 to "__call__" of "ufunc" has incompatible type
779-
# "Union[ExtensionArray, Any]"; expected "Union[Union[int, float, complex, str,
780-
# bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]],
781-
# Sequence[Sequence[Any]], _SupportsArray]"
782-
checker(arr[i : i + chunk_len]).all() # type: ignore[arg-type]
783-
for i in range(0, total_len, chunk_len)
778+
checker(arr[i : i + chunk_len]).all() for i in range(0, total_len, chunk_len)
784779
)

pandas/core/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,9 @@ def size(self) -> int:
714714
>>> df.size
715715
4
716716
"""
717-
return np.prod(self.shape)
717+
# error: Incompatible return value type (got "signedinteger[_64Bit]",
718+
# expected "int") [return-value]
719+
return np.prod(self.shape) # type: ignore[return-value]
718720

719721
@overload
720722
def set_axis(

pandas/core/groupby/generic.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,12 @@ def value_counts(
687687

688688
# multi-index components
689689
codes = self.grouper.reconstructed_codes
690-
codes = [rep(level_codes) for level_codes in codes] + [llab(lab, inc)]
690+
# error: Incompatible types in assignment (expression has type
691+
# "List[ndarray[Any, dtype[_SCT]]]",
692+
# variable has type "List[ndarray[Any, dtype[signedinteger[Any]]]]")
693+
codes = [ # type: ignore[assignment]
694+
rep(level_codes) for level_codes in codes
695+
] + [llab(lab, inc)]
691696
# error: List item 0 has incompatible type "Union[ndarray[Any, Any], Index]";
692697
# expected "Index"
693698
levels = [ping.group_index for ping in self.grouper.groupings] + [

pandas/core/indexes/interval.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,13 @@ def interval_range(
11101110
if all(is_integer(x) for x in com.not_none(start, end, freq)):
11111111
# np.linspace always produces float output
11121112

1113-
breaks = maybe_downcast_numeric(breaks, np.dtype("int64"))
1113+
# error: Argument 1 to "maybe_downcast_numeric" has incompatible type
1114+
# "Union[ndarray[Any, Any], TimedeltaIndex, DatetimeIndex]";
1115+
# expected "ndarray[Any, Any]" [
1116+
breaks = maybe_downcast_numeric(
1117+
breaks, # type: ignore[arg-type]
1118+
np.dtype("int64"),
1119+
)
11141120
else:
11151121
# delegate to the appropriate range function
11161122
if isinstance(endpoint, Timestamp):

pandas/core/resample.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,9 @@ def _get_period_bins(self, ax: PeriodIndex):
19021902
# NaT handling as in pandas._lib.lib.generate_bins_dt64()
19031903
nat_count = 0
19041904
if memb.hasnans:
1905-
nat_count = np.sum(memb._isnan)
1905+
# error: Incompatible types in assignment (expression has type
1906+
# "bool_", variable has type "int") [assignment]
1907+
nat_count = np.sum(memb._isnan) # type: ignore[assignment]
19061908
memb = memb[~memb._isnan]
19071909

19081910
if not len(memb):

pandas/core/reshape/merge.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -1301,13 +1301,7 @@ def _maybe_coerce_merge_keys(self) -> None:
13011301
# "Union[dtype[Any], Type[Any], _SupportsDType[dtype[Any]]]"
13021302
casted = lk.astype(rk.dtype) # type: ignore[arg-type]
13031303

1304-
# Argument 1 to "__call__" of "_UFunc_Nin1_Nout1" has
1305-
# incompatible type "Union[ExtensionArray, ndarray[Any, Any],
1306-
# Index, Series]"; expected "Union[_SupportsArray[dtype[Any]],
1307-
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int,
1308-
# float, complex, str, bytes, _NestedSequence[Union[bool,
1309-
# int, float, complex, str, bytes]]]"
1310-
mask = ~np.isnan(lk) # type: ignore[arg-type]
1304+
mask = ~np.isnan(lk)
13111305
match = lk == casted
13121306
# error: Item "ExtensionArray" of "Union[ExtensionArray,
13131307
# ndarray[Any, Any], Any]" has no attribute "all"
@@ -1329,13 +1323,7 @@ def _maybe_coerce_merge_keys(self) -> None:
13291323
# "Union[dtype[Any], Type[Any], _SupportsDType[dtype[Any]]]"
13301324
casted = rk.astype(lk.dtype) # type: ignore[arg-type]
13311325

1332-
# Argument 1 to "__call__" of "_UFunc_Nin1_Nout1" has
1333-
# incompatible type "Union[ExtensionArray, ndarray[Any, Any],
1334-
# Index, Series]"; expected "Union[_SupportsArray[dtype[Any]],
1335-
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int,
1336-
# float, complex, str, bytes, _NestedSequence[Union[bool,
1337-
# int, float, complex, str, bytes]]]"
1338-
mask = ~np.isnan(rk) # type: ignore[arg-type]
1326+
mask = ~np.isnan(rk)
13391327
match = rk == casted
13401328
# error: Item "ExtensionArray" of "Union[ExtensionArray,
13411329
# ndarray[Any, Any], Any]" has no attribute "all"

pandas/core/reshape/util.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ def cartesian_product(X) -> list[np.ndarray]:
5555
# if any factor is empty, the cartesian product is empty
5656
b = np.zeros_like(cumprodX)
5757

58-
return [tile_compat(np.repeat(x, b[i]), np.product(a[i])) for i, x in enumerate(X)]
58+
# error: Argument of type "int_" cannot be assigned to parameter "num" of
59+
# type "int" in function "tile_compat"
60+
return [
61+
tile_compat(
62+
np.repeat(x, b[i]),
63+
np.product(a[i]), # pyright: ignore[reportGeneralTypeIssues]
64+
)
65+
for i, x in enumerate(X)
66+
]
5967

6068

6169
def tile_compat(arr: NumpyIndexT, num: int) -> NumpyIndexT:

0 commit comments

Comments
 (0)