Skip to content

Commit 2d9e080

Browse files
authored
Backport PR #48850 on branch 1.5.x (TYP: Fix typing errors caused by new numpy) (#48859)
1 parent 7078607 commit 2d9e080

File tree

11 files changed

+49
-18
lines changed

11 files changed

+49
-18
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
@@ -540,7 +540,9 @@ def size(self) -> int:
540540
"""
541541
The number of elements in the array.
542542
"""
543-
return np.prod(self.shape)
543+
# error: Incompatible return value type (got "signedinteger[_64Bit]",
544+
# expected "int") [return-value]
545+
return np.prod(self.shape) # type: ignore[return-value]
544546

545547
@property
546548
def ndim(self) -> int:

pandas/core/arrays/sparse/accessor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ def density(self) -> float:
369369
Ratio of non-sparse points to total (dense) data points.
370370
"""
371371
tmp = np.mean([column.array.density for _, column in self._parent.items()])
372-
return tmp
372+
# error: Expression of type "floating" cannot be assigned to return type "float"
373+
return tmp # pyright: ignore[reportGeneralTypeIssues]
373374

374375
@staticmethod
375376
def _prep_index(data, index, columns):

pandas/core/arrays/sparse/array.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1424,8 +1424,7 @@ def __setstate__(self, state) -> None:
14241424
if isinstance(state, tuple):
14251425
# Compat for pandas < 0.24.0
14261426
nd_state, (fill_value, sp_index) = state
1427-
# error: Need type annotation for "sparse_values"
1428-
sparse_values = np.array([]) # type: ignore[var-annotated]
1427+
sparse_values = np.array([])
14291428
sparse_values.__setstate__(nd_state)
14301429

14311430
self._sparse_values = sparse_values

pandas/core/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
from pandas._typing import (
8181
NumpySorter,
8282
NumpyValueArrayLike,
83+
ScalarLike_co,
8384
)
8485

8586
from pandas import (
@@ -1267,7 +1268,7 @@ def factorize(
12671268
# return types [misc]
12681269
def searchsorted( # type: ignore[misc]
12691270
self,
1270-
value: npt._ScalarLike_co,
1271+
value: ScalarLike_co,
12711272
side: Literal["left", "right"] = ...,
12721273
sorter: NumpySorter = ...,
12731274
) -> 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
@@ -707,7 +707,9 @@ def size(self) -> int:
707707
>>> df.size
708708
4
709709
"""
710-
return np.prod(self.shape)
710+
# error: Incompatible return value type (got "signedinteger[_64Bit]",
711+
# expected "int") [return-value]
712+
return np.prod(self.shape) # type: ignore[return-value]
711713

712714
@overload
713715
def set_axis(

pandas/core/groupby/generic.py

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

692692
# multi-index components
693693
codes = self.grouper.reconstructed_codes
694-
codes = [rep(level_codes) for level_codes in codes] + [llab(lab, inc)]
694+
# error: Incompatible types in assignment (expression has type
695+
# "List[ndarray[Any, dtype[_SCT]]]",
696+
# variable has type "List[ndarray[Any, dtype[signedinteger[Any]]]]")
697+
codes = [ # type: ignore[assignment]
698+
rep(level_codes) for level_codes in codes
699+
] + [llab(lab, inc)]
695700
# error: List item 0 has incompatible type "Union[ndarray[Any, Any], Index]";
696701
# expected "Index"
697702
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
@@ -1886,7 +1886,9 @@ def _get_period_bins(self, ax: PeriodIndex):
18861886
# NaT handling as in pandas._lib.lib.generate_bins_dt64()
18871887
nat_count = 0
18881888
if memb.hasnans:
1889-
nat_count = np.sum(memb._isnan)
1889+
# error: Incompatible types in assignment (expression has type
1890+
# "bool_", variable has type "int") [assignment]
1891+
nat_count = np.sum(memb._isnan) # type: ignore[assignment]
18901892
memb = memb[~memb._isnan]
18911893

18921894
if not len(memb):

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)