Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #48850 on branch 1.5.x (TYP: Fix typing errors caused by new numpy) #48859

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,19 @@

from pandas.io.formats.format import EngFormatter

ScalarLike_co = Union[
int,
float,
complex,
str,
bytes,
np.generic,
]

# numpy compatible types
NumpyValueArrayLike = Union[npt._ScalarLike_co, npt.ArrayLike]
NumpySorter = Optional[npt._ArrayLikeInt_co]
NumpyValueArrayLike = Union[ScalarLike_co, npt.ArrayLike]
# Name "npt._ArrayLikeInt_co" is not defined [name-defined]
NumpySorter = Optional[npt._ArrayLikeInt_co] # type: ignore[name-defined]

else:
npt: Any = None
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ def size(self) -> int:
"""
The number of elements in the array.
"""
return np.prod(self.shape)
# error: Incompatible return value type (got "signedinteger[_64Bit]",
# expected "int") [return-value]
return np.prod(self.shape) # type: ignore[return-value]

@property
def ndim(self) -> int:
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ def density(self) -> float:
Ratio of non-sparse points to total (dense) data points.
"""
tmp = np.mean([column.array.density for _, column in self._parent.items()])
return tmp
# error: Expression of type "floating" cannot be assigned to return type "float"
return tmp # pyright: ignore[reportGeneralTypeIssues]

@staticmethod
def _prep_index(data, index, columns):
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,8 +1424,7 @@ def __setstate__(self, state) -> None:
if isinstance(state, tuple):
# Compat for pandas < 0.24.0
nd_state, (fill_value, sp_index) = state
# error: Need type annotation for "sparse_values"
sparse_values = np.array([]) # type: ignore[var-annotated]
sparse_values = np.array([])
sparse_values.__setstate__(nd_state)

self._sparse_values = sparse_values
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
from pandas._typing import (
NumpySorter,
NumpyValueArrayLike,
ScalarLike_co,
)

from pandas import (
Expand Down Expand Up @@ -1267,7 +1268,7 @@ def factorize(
# return types [misc]
def searchsorted( # type: ignore[misc]
self,
value: npt._ScalarLike_co,
value: ScalarLike_co,
side: Literal["left", "right"] = ...,
sorter: NumpySorter = ...,
) -> np.intp:
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,5 @@ def isna_all(arr: ArrayLike) -> bool:
)

return all(
# error: Argument 1 to "__call__" of "ufunc" has incompatible type
# "Union[ExtensionArray, Any]"; expected "Union[Union[int, float, complex, str,
# bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]],
# Sequence[Sequence[Any]], _SupportsArray]"
checker(arr[i : i + chunk_len]).all() # type: ignore[arg-type]
for i in range(0, total_len, chunk_len)
checker(arr[i : i + chunk_len]).all() for i in range(0, total_len, chunk_len)
)
4 changes: 3 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,9 @@ def size(self) -> int:
>>> df.size
4
"""
return np.prod(self.shape)
# error: Incompatible return value type (got "signedinteger[_64Bit]",
# expected "int") [return-value]
return np.prod(self.shape) # type: ignore[return-value]

@overload
def set_axis(
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,12 @@ def value_counts(

# multi-index components
codes = self.grouper.reconstructed_codes
codes = [rep(level_codes) for level_codes in codes] + [llab(lab, inc)]
# error: Incompatible types in assignment (expression has type
# "List[ndarray[Any, dtype[_SCT]]]",
# variable has type "List[ndarray[Any, dtype[signedinteger[Any]]]]")
codes = [ # type: ignore[assignment]
rep(level_codes) for level_codes in codes
] + [llab(lab, inc)]
# error: List item 0 has incompatible type "Union[ndarray[Any, Any], Index]";
# expected "Index"
levels = [ping.group_index for ping in self.grouper.groupings] + [
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,13 @@ def interval_range(
if all(is_integer(x) for x in com.not_none(start, end, freq)):
# np.linspace always produces float output

breaks = maybe_downcast_numeric(breaks, np.dtype("int64"))
# error: Argument 1 to "maybe_downcast_numeric" has incompatible type
# "Union[ndarray[Any, Any], TimedeltaIndex, DatetimeIndex]";
# expected "ndarray[Any, Any]" [
breaks = maybe_downcast_numeric(
breaks, # type: ignore[arg-type]
np.dtype("int64"),
)
else:
# delegate to the appropriate range function
if isinstance(endpoint, Timestamp):
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,9 @@ def _get_period_bins(self, ax: PeriodIndex):
# NaT handling as in pandas._lib.lib.generate_bins_dt64()
nat_count = 0
if memb.hasnans:
nat_count = np.sum(memb._isnan)
# error: Incompatible types in assignment (expression has type
# "bool_", variable has type "int") [assignment]
nat_count = np.sum(memb._isnan) # type: ignore[assignment]
memb = memb[~memb._isnan]

if not len(memb):
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/reshape/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ def cartesian_product(X) -> list[np.ndarray]:
# if any factor is empty, the cartesian product is empty
b = np.zeros_like(cumprodX)

return [tile_compat(np.repeat(x, b[i]), np.product(a[i])) for i, x in enumerate(X)]
# error: Argument of type "int_" cannot be assigned to parameter "num" of
# type "int" in function "tile_compat"
return [
tile_compat(
np.repeat(x, b[i]),
np.product(a[i]), # pyright: ignore[reportGeneralTypeIssues]
)
for i, x in enumerate(X)
]


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