diff --git a/pandas-stubs/_libs/tslibs/period.pyi b/pandas-stubs/_libs/tslibs/period.pyi index 00847c217..7c5671e6e 100644 --- a/pandas-stubs/_libs/tslibs/period.pyi +++ b/pandas-stubs/_libs/tslibs/period.pyi @@ -171,8 +171,6 @@ class Period(PeriodMixin): @overload def __radd__(self, other: Index) -> Index: ... @overload - def __radd__(self, other: Series[Timedelta]) -> PeriodSeries: ... - @overload def __radd__(self, other: NaTType) -> NaTType: ... @property def day(self) -> int: ... diff --git a/pandas-stubs/_libs/tslibs/timedeltas.pyi b/pandas-stubs/_libs/tslibs/timedeltas.pyi index a8f03fbcb..9ad5e0e68 100644 --- a/pandas-stubs/_libs/tslibs/timedeltas.pyi +++ b/pandas-stubs/_libs/tslibs/timedeltas.pyi @@ -137,9 +137,9 @@ class Timedelta(timedelta): def resolution_string(self) -> str: ... # Override due to more types supported than dt.timedelta @overload # type: ignore[override] - def __add__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ... + def __add__(self, other: dt.datetime | np.datetime64) -> Timestamp: ... @overload - def __add__(self, other: dt.datetime | np.datetime64 | Timestamp) -> Timestamp: ... + def __add__(self, other: timedelta | np.timedelta64) -> Self: ... @overload def __add__(self, other: NaTType) -> NaTType: ... @overload @@ -147,10 +147,6 @@ class Timedelta(timedelta): @overload def __add__(self, other: dt.date) -> dt.date: ... @overload - def __add__(self, other: PeriodIndex) -> PeriodIndex: ... - @overload - def __add__(self, other: DatetimeIndex) -> DatetimeIndex: ... - @overload def __add__( self, other: np_ndarray[ShapeT, np.timedelta64] ) -> np_ndarray[ShapeT, np.timedelta64]: ... @@ -159,18 +155,14 @@ class Timedelta(timedelta): self, other: np_ndarray[ShapeT, np.datetime64] ) -> np_ndarray[ShapeT, np.datetime64]: ... @overload - def __add__(self, other: pd.TimedeltaIndex) -> pd.TimedeltaIndex: ... - @overload - def __add__(self, other: Series[Timedelta]) -> Series[Timedelta]: ... - @overload - def __add__(self, other: Series[Timestamp]) -> Series[Timestamp]: ... + def __radd__(self, other: dt.datetime | np.datetime64) -> Timestamp: ... # type: ignore[misc] @overload - def __radd__(self, other: np.datetime64) -> Timestamp: ... - @overload - def __radd__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ... + def __radd__(self, other: timedelta | np.timedelta64) -> Self: ... @overload def __radd__(self, other: NaTType) -> NaTType: ... @overload + def __radd__(self, other: dt.date) -> dt.date: ... + @overload def __radd__( self, other: np_ndarray[ShapeT, np.timedelta64] ) -> np_ndarray[ShapeT, np.timedelta64]: ... @@ -178,10 +170,6 @@ class Timedelta(timedelta): def __radd__( self, other: np_ndarray[ShapeT, np.datetime64] ) -> np_ndarray[ShapeT, np.datetime64]: ... - @overload - def __radd__(self, other: pd.TimedeltaIndex) -> pd.TimedeltaIndex: ... - @overload - def __radd__(self, other: pd.PeriodIndex) -> pd.PeriodIndex: ... # Override due to more types supported than dt.timedelta @overload # type: ignore[override] def __sub__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ... diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 95e184356..b852fcc65 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -847,25 +847,33 @@ MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool] T_INT = TypeVar("T_INT", bound=int) T_COMPLEX = TypeVar("T_COMPLEX", bound=complex) -SeriesDType: TypeAlias = ( +SeriesDTypeNoDateTime: TypeAlias = ( str | bytes - | datetime.date - | datetime.time | bool | int | float | complex | Dtype - | datetime.datetime # includes pd.Timestamp - | datetime.timedelta # includes pd.Timedelta | Period | Interval | CategoricalDtype | BaseOffset | list[str] ) +SeriesDType: TypeAlias = ( + SeriesDTypeNoDateTime + | datetime.date + | datetime.time + | datetime.datetime # includes pd.Timestamp + | datetime.timedelta # includes pd.Timedelta +) S1 = TypeVar("S1", bound=SeriesDType, default=Any) +S1_CT_NDT = TypeVar( + "S1_CT_NDT", bound=SeriesDTypeNoDateTime, default=Any, contravariant=True +) +S1_CO = TypeVar("S1_CO", bound=SeriesDType, default=Any, covariant=True) +S1_CT = TypeVar("S1_CT", bound=SeriesDType, default=Any, contravariant=True) # Like S1, but without `default=Any`. S2 = TypeVar("S2", bound=SeriesDType) S3 = TypeVar("S3", bound=SeriesDType) diff --git a/pandas-stubs/core/indexes/base.pyi b/pandas-stubs/core/indexes/base.pyi index 034b04cc5..d98bb6e15 100644 --- a/pandas-stubs/core/indexes/base.pyi +++ b/pandas-stubs/core/indexes/base.pyi @@ -3,7 +3,6 @@ from collections.abc import ( Callable, Hashable, Iterable, - Iterator, Sequence, ) from datetime import ( @@ -20,6 +19,10 @@ from typing import ( type_check_only, ) +from _typeshed import ( + SupportsAdd, + SupportsRAdd, +) import numpy as np from pandas import ( DataFrame, @@ -49,8 +52,9 @@ from pandas._libs.interval import _OrderableT from pandas._typing import ( C2, S1, + S1_CO, + S1_CT, T_COMPLEX, - T_INT, AnyAll, ArrayLike, AxesData, @@ -471,7 +475,6 @@ class Index(IndexOpsMixin[S1]): def shape(self) -> tuple[int, ...]: ... # Extra methods from old stubs def __eq__(self, other: object) -> np_1darray[np.bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] - def __iter__(self) -> Iterator[S1]: ... def __ne__(self, other: object) -> np_1darray[np.bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] def __le__(self, other: Self | S1) -> np_1darray[np.bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] def __ge__(self, other: Self | S1) -> np_1darray[np.bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] @@ -484,65 +487,43 @@ class Index(IndexOpsMixin[S1]): @overload def __add__(self, other: Index[Never]) -> Index: ... @overload - def __add__( - self: Index[bool], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX], - ) -> Index[T_COMPLEX]: ... - @overload - def __add__(self: Index[bool], other: np_ndarray_bool) -> Index[bool]: ... + def __add__(self: Index[bool], other: bool | Sequence[bool]) -> Index[bool]: ... @overload - def __add__(self: Index[bool], other: np_ndarray_anyint) -> Index[int]: ... + def __add__(self: Index[int], other: bool | Sequence[bool]) -> Index[int]: ... @overload - def __add__(self: Index[bool], other: np_ndarray_float) -> Index[float]: ... + def __add__(self: Index[float], other: int | Sequence[int]) -> Index[float]: ... @overload - def __add__(self: Index[bool], other: np_ndarray_complex) -> Index[complex]: ... + def __add__( + self: Index[complex], other: float | Sequence[float] + ) -> Index[complex]: ... @overload def __add__( - self: Index[int], - other: ( - bool | Sequence[bool] | np_ndarray_bool | np_ndarray_anyint | Index[bool] - ), - ) -> Index[int]: ... + self: Index[S1_CT], + other: SupportsRAdd[S1_CT, S1_CO] | Sequence[SupportsRAdd[S1_CT, S1_CO]], + ) -> Index[S1_CO]: ... @overload def __add__( - self: Index[int], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX], + self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool] ) -> Index[T_COMPLEX]: ... @overload - def __add__(self: Index[int], other: np_ndarray_float) -> Index[float]: ... + def __add__( + self: Index[bool], other: np_ndarray_anyint | Index[int] + ) -> Index[int]: ... @overload - def __add__(self: Index[int], other: np_ndarray_complex) -> Index[complex]: ... + def __add__( + self: Index[T_COMPLEX], other: np_ndarray_anyint | Index[int] + ) -> Index[T_COMPLEX]: ... @overload def __add__( - self: Index[float], - other: ( - int - | Sequence[int] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_INT] - ), + self: Index[bool] | Index[int], other: np_ndarray_float | Index[float] ) -> Index[float]: ... @overload def __add__( - self: Index[float], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX], + self: Index[T_COMPLEX], other: np_ndarray_float | Index[float] ) -> Index[T_COMPLEX]: ... @overload - def __add__(self: Index[float], other: np_ndarray_complex) -> Index[complex]: ... - @overload def __add__( - self: Index[complex], - other: ( - T_COMPLEX - | Sequence[T_COMPLEX] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | np_ndarray_complex - | Index[T_COMPLEX] - ), + self: Index[T_COMPLEX], other: np_ndarray_complex | Index[complex] ) -> Index[complex]: ... @overload def __add__( @@ -560,60 +541,43 @@ class Index(IndexOpsMixin[S1]): @overload def __radd__(self: Index[Never], other: complex | _ListLike | Index) -> Index: ... @overload - def __radd__( - self: Index[bool], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX], - ) -> Index[T_COMPLEX]: ... + def __radd__(self: Index[bool], other: bool | Sequence[bool]) -> Index[bool]: ... @overload - def __radd__(self: Index[bool], other: np_ndarray_bool) -> Index[bool]: ... + def __radd__(self: Index[int], other: bool | Sequence[bool]) -> Index[int]: ... @overload - def __radd__(self: Index[bool], other: np_ndarray_anyint) -> Index[int]: ... + def __radd__(self: Index[float], other: int | Sequence[int]) -> Index[float]: ... @overload - def __radd__(self: Index[bool], other: np_ndarray_float) -> Index[float]: ... + def __radd__( + self: Index[complex], other: float | Sequence[float] + ) -> Index[complex]: ... @overload def __radd__( - self: Index[int], - other: ( - bool | Sequence[bool] | np_ndarray_bool | np_ndarray_anyint | Index[bool] - ), - ) -> Index[int]: ... + self: Index[S1_CT], + other: SupportsAdd[S1_CT, S1_CO] | Sequence[SupportsAdd[S1_CT, S1_CO]], + ) -> Index[S1_CO]: ... @overload def __radd__( - self: Index[int], other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] + self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool] ) -> Index[T_COMPLEX]: ... @overload - def __radd__(self: Index[int], other: np_ndarray_float) -> Index[float]: ... - @overload def __radd__( - self: Index[float], - other: ( - int - | Sequence[int] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_INT] - ), - ) -> Index[float]: ... + self: Index[bool], other: np_ndarray_anyint | Index[int] + ) -> Index[int]: ... @overload def __radd__( - self: Index[float], other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] + self: Index[T_COMPLEX], other: np_ndarray_anyint | Index[int] ) -> Index[T_COMPLEX]: ... @overload def __radd__( - self: Index[complex], - other: ( - T_COMPLEX - | Sequence[T_COMPLEX] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_COMPLEX] - ), - ) -> Index[complex]: ... + self: Index[bool] | Index[int], other: np_ndarray_float | Index[float] + ) -> Index[float]: ... + @overload + def __radd__( + self: Index[T_COMPLEX], other: np_ndarray_float | Index[float] + ) -> Index[T_COMPLEX]: ... @overload def __radd__( - self: Index[T_COMPLEX], other: np_ndarray_complex + self: Index[T_COMPLEX], other: np_ndarray_complex | Index[complex] ) -> Index[complex]: ... @overload def __radd__( diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index 3f882c808..42b2f2d4d 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -66,8 +66,11 @@ class DatetimeIndex( # various ignores needed for mypy, as we do want to restrict what can be used in # arithmetic for these types - def __add__( # pyright: ignore[reportIncompatibleMethodOverride] - self, other: timedelta | Timedelta | TimedeltaIndex | BaseOffset # type: ignore[override] + def __add__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] + self, other: timedelta | TimedeltaIndex | BaseOffset + ) -> DatetimeIndex: ... + def __radd__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] + self, other: timedelta | TimedeltaIndex | BaseOffset ) -> DatetimeIndex: ... @overload # type: ignore[override] def __sub__( diff --git a/pandas-stubs/core/indexes/period.pyi b/pandas-stubs/core/indexes/period.pyi index 98c4efad4..158f38698 100644 --- a/pandas-stubs/core/indexes/period.pyi +++ b/pandas-stubs/core/indexes/period.pyi @@ -36,6 +36,12 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField ) -> Self: ... @property def values(self) -> np_1darray[np.object_]: ... + def __add__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] + self, other: datetime.timedelta + ) -> Self: ... + def __radd__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] + self, other: datetime.timedelta + ) -> Self: ... @overload # type: ignore[override] def __sub__(self, other: Period) -> Index: ... @overload diff --git a/pandas-stubs/core/indexes/timedeltas.pyi b/pandas-stubs/core/indexes/timedeltas.pyi index 0b3392f7c..89866d48f 100644 --- a/pandas-stubs/core/indexes/timedeltas.pyi +++ b/pandas-stubs/core/indexes/timedeltas.pyi @@ -22,10 +22,7 @@ from pandas.core.indexes.period import PeriodIndex from pandas.core.series import Series from typing_extensions import Self -from pandas._libs import ( - Timedelta, - Timestamp, -) +from pandas._libs import Timedelta from pandas._libs.tslibs import BaseOffset from pandas._typing import ( AxesData, @@ -53,12 +50,19 @@ class TimedeltaIndex( @overload # type: ignore[override] def __add__(self, other: Period) -> PeriodIndex: ... @overload - def __add__(self, other: DatetimeIndex) -> DatetimeIndex: ... + def __add__(self, other: dt.datetime | DatetimeIndex) -> DatetimeIndex: ... @overload def __add__( # pyright: ignore[reportIncompatibleMethodOverride] - self, other: dt.timedelta | Timedelta | Self + self, other: dt.timedelta | Self + ) -> Self: ... + @overload # type: ignore[override] + def __radd__(self, other: Period) -> PeriodIndex: ... + @overload + def __radd__(self, other: dt.datetime | DatetimeIndex) -> DatetimeIndex: ... + @overload + def __radd__( # pyright: ignore[reportIncompatibleMethodOverride] + self, other: dt.timedelta | Self ) -> Self: ... - def __radd__(self, other: dt.datetime | Timestamp | DatetimeIndex) -> DatetimeIndex: ... # type: ignore[override] def __sub__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] self, other: dt.timedelta | np.timedelta64 | np_ndarray_td | Self ) -> Self: ... diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 96509c4c6..2af21e434 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -32,7 +32,11 @@ from typing import ( type_check_only, ) -from _typeshed import SupportsGetItem +from _typeshed import ( + SupportsAdd, + SupportsGetItem, + SupportsRAdd, +) from matplotlib.axes import ( Axes as PlotAxes, SubplotBase, @@ -110,6 +114,9 @@ from pandas._libs.tslibs.nattype import NaTType from pandas._libs.tslibs.offsets import DateOffset from pandas._typing import ( S1, + S1_CO, + S1_CT, + S1_CT_NDT, S2, T_COMPLEX, T_INT, @@ -1656,70 +1663,83 @@ class Series(IndexOpsMixin[S1], NDFrame): @overload def __add__(self, other: Index[Never] | Series[Never]) -> Series: ... @overload - def __add__( - self: Series[bool], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], - ) -> Series[T_COMPLEX]: ... - @overload - def __add__(self: Series[bool], other: np_ndarray_bool) -> Series[bool]: ... + def __add__(self: Series[Timestamp], other: np_ndarray_dt) -> Never: ... @overload - def __add__(self: Series[bool], other: np_ndarray_anyint) -> Series[int]: ... + def __add__( + self: Series[Timestamp], + other: ( + timedelta + | np.timedelta64 + | np_ndarray_td + | TimedeltaIndex + | Series[Timedelta] + | BaseOffset + ), + ) -> Series[Timestamp]: ... @overload - def __add__(self: Series[bool], other: np_ndarray_float) -> Series[float]: ... + def __add__( + self: Series[Timedelta], + other: ( + datetime | np.datetime64 | np_ndarray_dt | DatetimeIndex | Series[Timestamp] + ), + ) -> Series[Timestamp]: ... @overload def __add__( - self: Series[int], + self: Series[Timedelta], other: ( - bool - | Sequence[bool] - | np_ndarray_bool - | np_ndarray_anyint - | Index[bool] - | Series[bool] + timedelta + | np.timedelta64 + | np_ndarray_td + | TimedeltaIndex + | Series[Timedelta] ), - ) -> Series[int]: ... + ) -> Series[Timedelta]: ... + @overload + def __add__(self: Series[Timedelta], other: Period) -> PeriodSeries: ... + @overload + def __add__(self: Series[bool], other: bool | Sequence[bool]) -> Series[bool]: ... + @overload + def __add__(self: Series[int], other: bool | Sequence[bool]) -> Series[int]: ... + @overload + def __add__(self: Series[float], other: int | Sequence[int]) -> Series[float]: ... @overload def __add__( - self: Series[int], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], - ) -> Series[T_COMPLEX]: ... + self: Series[complex], other: float | Sequence[float] + ) -> Series[complex]: ... @overload - def __add__(self: Series[int], other: np_ndarray_float) -> Series[float]: ... + def __add__( + self: Series[S1_CT], other: SupportsRAdd[S1_CT, S1_CO] + ) -> Series[S1_CO]: ... + # pandas-dev/pandas#62353 @overload def __add__( - self: Series[float], - other: ( - int - | Sequence[int] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_INT] - | Series[T_INT] - ), - ) -> Series[float]: ... + self: Series[S1_CT_NDT], other: Sequence[SupportsRAdd[S1_CT_NDT, S1_CO]] + ) -> Series[S1_CO]: ... @overload def __add__( - self: Series[float], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], + self: Series[T_COMPLEX], other: np_ndarray_bool | Index[bool] | Series[bool] ) -> Series[T_COMPLEX]: ... @overload def __add__( - self: Series[complex], - other: ( - T_COMPLEX - | Sequence[T_COMPLEX] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | np_ndarray_complex - | Index[T_COMPLEX] - | Series[T_COMPLEX] - ), - ) -> Series[complex]: ... + self: Series[bool], other: np_ndarray_anyint | Index[int] | Series[int] + ) -> Series[int]: ... @overload def __add__( - self: Series[T_COMPLEX], other: np_ndarray_complex + self: Series[T_COMPLEX], other: np_ndarray_anyint | Index[int] | Series[int] + ) -> Series[T_COMPLEX]: ... + @overload + def __add__( + self: Series[bool] | Series[int], + other: np_ndarray_float | Index[float] | Series[float], + ) -> Series[float]: ... + @overload + def __add__( + self: Series[T_COMPLEX], other: np_ndarray_float | Index[float] | Series[float] + ) -> Series[T_COMPLEX]: ... + @overload + def __add__( + self: Series[T_COMPLEX], + other: np_ndarray_complex | Index[complex] | Series[complex], ) -> Series[complex]: ... @overload def __add__( @@ -1734,162 +1754,158 @@ class Series(IndexOpsMixin[S1], NDFrame): other: _str | Sequence[_str] | np_ndarray_str | Index[_str] | Series[_str], ) -> Series[_str]: ... @overload - def __add__(self: Series[Timestamp], other: np_ndarray_dt) -> Never: ... + def add( + self: Series[Never], + other: complex | _ListLike | Index | Series, + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, + ) -> Series: ... @overload - def __add__( + def add( + self, + other: Index[Never] | Series[Never], + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, + ) -> Series: ... + @overload + def add( self: Series[Timestamp], other: ( timedelta + | Sequence[timedelta] | np.timedelta64 | np_ndarray_td | TimedeltaIndex | Series[Timedelta] | BaseOffset ), + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, ) -> Series[Timestamp]: ... @overload - def __add__( + def add( self: Series[Timedelta], other: ( - datetime | np.datetime64 | np_ndarray_dt | DatetimeIndex | Series[Timestamp] + datetime + | Sequence[datetime] + | np.datetime64 + | np_ndarray_dt + | DatetimeIndex + | Series[Timestamp] ), + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, ) -> Series[Timestamp]: ... @overload - def __add__( + def add( self: Series[Timedelta], other: ( timedelta + | Sequence[timedelta] | np.timedelta64 | np_ndarray_td | TimedeltaIndex | Series[Timedelta] ), - ) -> Series[Timedelta]: ... - @overload - def __add__(self: Series[Timedelta], other: Period) -> PeriodSeries: ... - @overload - def add( - self: Series[Never], - other: complex | _ListLike | Index | Series, level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series: ... + ) -> Series[Timedelta]: ... @overload def add( - self, - other: Index[Never] | Series[Never], + self: Series[Timestamp], + other: Period, level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series: ... + ) -> PeriodSeries: ... @overload def add( self: Series[bool], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], + other: bool | Sequence[bool], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[T_COMPLEX]: ... + ) -> Series[bool]: ... @overload def add( - self: Series[bool], - other: np_ndarray_bool, + self: Series[int], + other: bool | Sequence[bool], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[bool]: ... + ) -> Series[int]: ... @overload def add( - self: Series[bool], - other: np_ndarray_anyint, + self: Series[float], + other: int | Sequence[int], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[int]: ... + ) -> Series[float]: ... @overload def add( - self: Series[bool], - other: np_ndarray_float, + self: Series[complex], + other: float | Sequence[float], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[float]: ... + ) -> Series[complex]: ... @overload def add( - self: Series[int], - other: ( - bool - | Sequence[bool] - | np_ndarray_bool - | np_ndarray_anyint - | Index[bool] - | Series[bool] - ), + self: Series[S1_CT], + other: SupportsRAdd[S1_CT, S1_CO] | Sequence[SupportsRAdd[S1_CT, S1_CO]], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[int]: ... + ) -> Series[S1_CO]: ... @overload def add( - self: Series[int], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], + self: Series[T_COMPLEX], + other: np_ndarray_bool | Index[bool] | Series[bool], level: Level | None = None, fill_value: float | None = None, axis: int = 0, ) -> Series[T_COMPLEX]: ... @overload def add( - self: Series[int], - other: np_ndarray_float, + self: Series[bool], + other: np_ndarray_anyint | Index[int] | Series[int], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[float]: ... + ) -> Series[int]: ... @overload def add( - self: Series[float], - other: ( - int - | Sequence[int] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_INT] - | Series[T_INT] - ), + self: Series[T_COMPLEX], + other: np_ndarray_anyint | Index[int] | Series[int], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[float]: ... + ) -> Series[T_COMPLEX]: ... @overload def add( - self: Series[float], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], - level: Level | None = None, + self: Series[bool] | Series[int], + other: np_ndarray_float | Index[float] | Series[float], fill_value: float | None = None, axis: int = 0, - ) -> Series[T_COMPLEX]: ... + ) -> Series[float]: ... @overload def add( - self: Series[complex], - other: ( - T_COMPLEX - | Sequence[T_COMPLEX] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_COMPLEX] - | Series[T_COMPLEX] - ), + self: Series[T_COMPLEX], + other: np_ndarray_float | Index[float] | Series[float], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[complex]: ... + ) -> Series[T_COMPLEX]: ... @overload def add( self: Series[T_COMPLEX], - other: np_ndarray_complex, + other: np_ndarray_complex | Index[complex] | Series[complex], level: Level | None = None, fill_value: float | None = None, axis: int = 0, @@ -1903,129 +1919,89 @@ class Series(IndexOpsMixin[S1], NDFrame): axis: int = 0, ) -> Series[_str]: ... @overload - def add( + def __radd__( + self: Series[Never], other: complex | _ListLike | Index | Series + ) -> Series: ... + @overload + def __radd__(self, other: Index[Never] | Series[Never]) -> Series: ... + @overload + def __radd__(self: Series[Timestamp], other: np_ndarray_dt) -> Never: ... + @overload + def __radd__( self: Series[Timestamp], other: ( timedelta - | Sequence[timedelta] | np.timedelta64 | np_ndarray_td | TimedeltaIndex | Series[Timedelta] | BaseOffset ), - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, ) -> Series[Timestamp]: ... @overload - def add( + def __radd__( self: Series[Timedelta], other: ( - datetime - | Sequence[datetime] - | np.datetime64 - | np_ndarray_dt - | DatetimeIndex - | Series[Timestamp] + datetime | np.datetime64 | np_ndarray_dt | DatetimeIndex | Series[Timestamp] ), - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, ) -> Series[Timestamp]: ... @overload - def add( + def __radd__( self: Series[Timedelta], other: ( timedelta - | Sequence[timedelta] | np.timedelta64 | np_ndarray_td | TimedeltaIndex | Series[Timedelta] ), - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, ) -> Series[Timedelta]: ... @overload - def add( - self: Series[Timestamp], - other: Period, - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, - ) -> PeriodSeries: ... + def __radd__(self: Series[Timedelta], other: Period) -> PeriodSeries: ... @overload - def __radd__( - self: Series[Never], other: complex | _ListLike | Index | Series - ) -> Series: ... + def __radd__(self: Series[bool], other: bool | Sequence[bool]) -> Series[bool]: ... @overload - def __radd__(self, other: Index[Never] | Series[Never]) -> Series: ... + def __radd__(self: Series[int], other: bool | Sequence[bool]) -> Series[int]: ... + @overload + def __radd__(self: Series[float], other: int | Sequence[int]) -> Series[float]: ... @overload def __radd__( - self: Series[bool], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], - ) -> Series[T_COMPLEX]: ... + self: Series[complex], other: float | Sequence[float] + ) -> Series[complex]: ... @overload - def __radd__(self: Series[bool], other: np_ndarray_bool) -> Series[bool]: ... + def __radd__( + self: Series[S1_CT], other: SupportsAdd[S1_CT, S1_CO] + ) -> Series[S1_CO]: ... + # pandas-dev/pandas#62353 @overload - def __radd__(self: Series[bool], other: np_ndarray_anyint) -> Series[int]: ... + def __radd__( + self: Series[S1_CT_NDT], other: Sequence[SupportsAdd[S1_CT_NDT, S1_CO]] + ) -> Series[S1_CO]: ... @overload - def __radd__(self: Series[bool], other: np_ndarray_float) -> Series[float]: ... + def __radd__( + self: Series[T_COMPLEX], other: np_ndarray_bool | Index[bool] | Series[bool] + ) -> Series[T_COMPLEX]: ... @overload def __radd__( - self: Series[int], - other: ( - bool - | Sequence[bool] - | np_ndarray_bool - | np_ndarray_anyint - | Index[bool] - | Series[bool] - ), + self: Series[bool], other: np_ndarray_anyint | Index[int] | Series[int] ) -> Series[int]: ... @overload def __radd__( - self: Series[int], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], + self: Series[T_COMPLEX], other: np_ndarray_anyint | Index[int] | Series[int] ) -> Series[T_COMPLEX]: ... @overload - def __radd__(self: Series[int], other: np_ndarray_float) -> Series[float]: ... - @overload def __radd__( - self: Series[float], - other: ( - int - | Sequence[int] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_INT] - | Series[T_INT] - ), + self: Series[bool] | Series[int], + other: np_ndarray_float | Index[float] | Series[float], ) -> Series[float]: ... @overload def __radd__( - self: Series[float], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], + self: Series[T_COMPLEX], other: np_ndarray_float | Index[float] | Series[float] ) -> Series[T_COMPLEX]: ... @overload def __radd__( - self: Series[complex], - other: ( - T_COMPLEX - | Sequence[T_COMPLEX] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_COMPLEX] - | Series[T_COMPLEX] - ), - ) -> Series[complex]: ... - @overload - def __radd__( - self: Series[T_COMPLEX], other: np_ndarray_complex + self: Series[T_COMPLEX], + other: np_ndarray_complex | Index[complex] | Series[complex], ) -> Series[complex]: ... @overload def __radd__( @@ -2040,162 +2016,158 @@ class Series(IndexOpsMixin[S1], NDFrame): other: _str | Sequence[_str] | np_ndarray_str | Index[_str] | Series[_str], ) -> Series[_str]: ... @overload - def __radd__(self: Series[Timestamp], other: np_ndarray_dt) -> Never: ... + def radd( + self: Series[Never], + other: complex | _ListLike | Index | Series, + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, + ) -> Series: ... @overload - def __radd__( + def radd( + self, + other: Index[Never] | Series[Never], + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, + ) -> Series: ... + @overload + def radd( self: Series[Timestamp], other: ( timedelta + | Sequence[timedelta] | np.timedelta64 | np_ndarray_td | TimedeltaIndex | Series[Timedelta] | BaseOffset ), + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, ) -> Series[Timestamp]: ... @overload - def __radd__( + def radd( self: Series[Timedelta], other: ( - datetime | np.datetime64 | np_ndarray_dt | DatetimeIndex | Series[Timestamp] + datetime + | Sequence[datetime] + | np.datetime64 + | np_ndarray_dt + | DatetimeIndex + | Series[Timestamp] ), + level: Level | None = None, + fill_value: float | None = None, + axis: int = 0, ) -> Series[Timestamp]: ... @overload - def __radd__( + def radd( self: Series[Timedelta], other: ( timedelta + | Sequence[timedelta] | np.timedelta64 | np_ndarray_td | TimedeltaIndex | Series[Timedelta] ), - ) -> Series[Timedelta]: ... - @overload - def __radd__(self: Series[Timedelta], other: Period) -> PeriodSeries: ... - @overload - def radd( - self: Series[Never], - other: complex | _ListLike | Index | Series, level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series: ... + ) -> Series[Timedelta]: ... @overload def radd( - self, - other: Index[Never] | Series[Never], + self: Series[Timestamp], + other: Period, level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series: ... + ) -> PeriodSeries: ... @overload def radd( self: Series[bool], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], + other: bool | Sequence[bool], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[T_COMPLEX]: ... + ) -> Series[bool]: ... @overload def radd( - self: Series[bool], - other: np_ndarray_bool, + self: Series[int], + other: bool | Sequence[bool], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[bool]: ... + ) -> Series[int]: ... @overload def radd( - self: Series[bool], - other: np_ndarray_anyint, + self: Series[float], + other: int | Sequence[int], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[int]: ... + ) -> Series[float]: ... @overload def radd( - self: Series[bool], - other: np_ndarray_float, + self: Series[complex], + other: float | Sequence[float], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[float]: ... + ) -> Series[complex]: ... @overload def radd( - self: Series[int], - other: ( - bool - | Sequence[bool] - | np_ndarray_bool - | np_ndarray_anyint - | Index[bool] - | Series[bool] - ), + self: Series[S1_CT], + other: SupportsAdd[S1_CT, S1_CO] | Sequence[SupportsAdd[S1_CT, S1_CO]], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[int]: ... + ) -> Series[S1_CO]: ... @overload def radd( - self: Series[int], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], + self: Series[T_COMPLEX], + other: np_ndarray_bool | Index[bool] | Series[bool], level: Level | None = None, fill_value: float | None = None, axis: int = 0, ) -> Series[T_COMPLEX]: ... @overload def radd( - self: Series[int], - other: np_ndarray_float, + self: Series[bool], + other: np_ndarray_anyint | Index[int] | Series[int], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[float]: ... + ) -> Series[int]: ... @overload def radd( - self: Series[float], - other: ( - int - | Sequence[int] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_INT] - | Series[T_INT] - ), + self: Series[T_COMPLEX], + other: np_ndarray_anyint | Index[int] | Series[int], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[float]: ... + ) -> Series[T_COMPLEX]: ... @overload def radd( - self: Series[float], - other: T_COMPLEX | Sequence[T_COMPLEX] | Index[T_COMPLEX] | Series[T_COMPLEX], - level: Level | None = None, + self: Series[bool] | Series[int], + other: np_ndarray_float | Index[float] | Series[float], fill_value: float | None = None, axis: int = 0, - ) -> Series[T_COMPLEX]: ... + ) -> Series[float]: ... @overload def radd( - self: Series[complex], - other: ( - T_COMPLEX - | Sequence[T_COMPLEX] - | np_ndarray_bool - | np_ndarray_anyint - | np_ndarray_float - | Index[T_COMPLEX] - | Series[T_COMPLEX] - ), + self: Series[T_COMPLEX], + other: np_ndarray_float | Index[float] | Series[float], level: Level | None = None, fill_value: float | None = None, axis: int = 0, - ) -> Series[complex]: ... + ) -> Series[T_COMPLEX]: ... @overload def radd( self: Series[T_COMPLEX], - other: np_ndarray_complex, + other: np_ndarray_complex | Index[complex] | Series[complex], level: Level | None = None, fill_value: float | None = None, axis: int = 0, @@ -2208,60 +2180,6 @@ class Series(IndexOpsMixin[S1], NDFrame): fill_value: float | None = None, axis: int = 0, ) -> Series[_str]: ... - @overload - def radd( - self: Series[Timestamp], - other: ( - timedelta - | Sequence[timedelta] - | np.timedelta64 - | np_ndarray_td - | TimedeltaIndex - | Series[Timedelta] - | BaseOffset - ), - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, - ) -> Series[Timestamp]: ... - @overload - def radd( - self: Series[Timedelta], - other: ( - datetime - | Sequence[datetime] - | np.datetime64 - | np_ndarray_dt - | DatetimeIndex - | Series[Timestamp] - ), - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, - ) -> Series[Timestamp]: ... - @overload - def radd( - self: Series[Timedelta], - other: ( - timedelta - | Sequence[timedelta] - | np.timedelta64 - | np_ndarray_td - | TimedeltaIndex - | Series[Timedelta] - ), - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, - ) -> Series[Timedelta]: ... - @overload - def radd( - self: Series[Timestamp], - other: Period, - level: Level | None = None, - fill_value: float | None = None, - axis: int = 0, - ) -> PeriodSeries: ... # ignore needed for mypy as we want different results based on the arguments @overload # type: ignore[override] def __and__( # pyright: ignore[reportOverlappingOverload] @@ -4760,7 +4678,6 @@ class Series(IndexOpsMixin[S1], NDFrame): inplace: Literal[False] = ..., ) -> Self: ... def set_axis(self, labels, *, axis: Axis = ..., copy: _bool = ...) -> Self: ... - def __iter__(self) -> Iterator[S1]: ... @final def xs( self, diff --git a/tests/series/arithmetic/str/test_add.py b/tests/series/arithmetic/str/test_add.py index d9b2ec6fd..78986f1ca 100644 --- a/tests/series/arithmetic/str/test_add.py +++ b/tests/series/arithmetic/str/test_add.py @@ -31,11 +31,11 @@ def test_add_py_scalar() -> None: check(assert_type(r0 + left, "pd.Series[str]"), pd.Series, str) if TYPE_CHECKING_INVALID_USAGE: - left.add(i) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + left.add(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] check(assert_type(left.add(r0), "pd.Series[str]"), pd.Series, str) if TYPE_CHECKING_INVALID_USAGE: - left.radd(i) # type: ignore[call-overload] # pyright: ignore[reportArgumentType, reportCallIssue] + left.radd(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType, reportCallIssue] check(assert_type(left.radd(r0), "pd.Series[str]"), pd.Series, str) diff --git a/tests/series/arithmetic/timestamp/test_add.py b/tests/series/arithmetic/timestamp/test_add.py index 3efdffe82..03cde2db0 100644 --- a/tests/series/arithmetic/timestamp/test_add.py +++ b/tests/series/arithmetic/timestamp/test_add.py @@ -33,11 +33,11 @@ def test_add_py_scalar() -> None: check(assert_type(d + left, "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) if TYPE_CHECKING_INVALID_USAGE: - left.add(s) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + left.add(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] check(assert_type(left.add(d), "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) if TYPE_CHECKING_INVALID_USAGE: - left.radd(s) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + left.radd(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] check(assert_type(left.radd(d), "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) @@ -55,11 +55,11 @@ def test_add_numpy_scalar() -> None: check(assert_type(d + left, "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) if TYPE_CHECKING_INVALID_USAGE: - left.add(s) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + left.add(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] check(assert_type(left.add(d), "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) if TYPE_CHECKING_INVALID_USAGE: - left.radd(s) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + left.radd(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] check(assert_type(left.radd(d), "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) @@ -77,11 +77,11 @@ def test_add_pd_scalar() -> None: check(assert_type(d + left, "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) if TYPE_CHECKING_INVALID_USAGE: - left.add(s) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + left.add(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] check(assert_type(left.add(d), "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp) if TYPE_CHECKING_INVALID_USAGE: - left.radd(s) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] + left.radd(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] check(assert_type(left.radd(d), "pd.Series[pd.Timestamp]"), pd.Series, pd.Timestamp)