Skip to content
Merged
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
@@ -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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the same (false positive?) issue as npt._ScalarLike_co - just out of interest, why redefine ScalarLike_co and ignore the error for _ArrayLikeInt_co?

Copy link
Member Author

@phofl phofl Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the scalar case was trivial while the array cases is defined using multiple private numpy typing objects

edit: we definitely should fix this at some point, but this is mainly to get ci running again

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok clear - no objections then

NumpySorter = Optional[npt._ArrayLikeInt_co] # type: ignore[name-defined]

else:
npt: Any = None
4 changes: 3 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
@@ -542,7 +542,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:
3 changes: 1 addition & 2 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
@@ -1426,8 +1426,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
3 changes: 2 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
@@ -84,6 +84,7 @@
DropKeep,
NumpySorter,
NumpyValueArrayLike,
ScalarLike_co,
)

from pandas import (
@@ -1275,7 +1276,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:
7 changes: 1 addition & 6 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
@@ -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
@@ -714,7 +714,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(
7 changes: 6 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
@@ -687,7 +687,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] + [
8 changes: 7 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
@@ -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):
4 changes: 3 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
@@ -1902,7 +1902,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):
16 changes: 2 additions & 14 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
@@ -1301,13 +1301,7 @@ def _maybe_coerce_merge_keys(self) -> None:
# "Union[dtype[Any], Type[Any], _SupportsDType[dtype[Any]]]"
casted = lk.astype(rk.dtype) # type: ignore[arg-type]

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

# Argument 1 to "__call__" of "_UFunc_Nin1_Nout1" has
# incompatible type "Union[ExtensionArray, ndarray[Any, Any],
# Index, Series]"; expected "Union[_SupportsArray[dtype[Any]],
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int,
# float, complex, str, bytes, _NestedSequence[Union[bool,
# int, float, complex, str, bytes]]]"
mask = ~np.isnan(rk) # type: ignore[arg-type]
mask = ~np.isnan(rk)
match = rk == casted
# error: Item "ExtensionArray" of "Union[ExtensionArray,
# ndarray[Any, Any], Any]" has no attribute "all"
10 changes: 9 additions & 1 deletion pandas/core/reshape/util.py
Original file line number Diff line number Diff line change
@@ -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: