Skip to content

Commit 4f11405

Browse files
committed
fix(comment): #1275 (review)
1 parent 2576640 commit 4f11405

File tree

5 files changed

+52
-80
lines changed

5 files changed

+52
-80
lines changed

pandas-stubs/core/series.pyi

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,23 +1704,40 @@ class Series(IndexOpsMixin[S1], NDFrame):
17041704
@overload
17051705
def __or__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ...
17061706
@overload
1707-
def __radd__(self: Series[Never], other: Scalar | _ListLike | Series) -> Series: ...
1707+
def __radd__(self: Series[Never], other: Scalar | _ListLike) -> Series: ...
17081708
@overload
17091709
def __radd__(
17101710
self: Series[int], other: _T_COMPLEX | Sequence[_T_COMPLEX]
17111711
) -> Series[_T_COMPLEX]: ...
17121712
@overload
1713-
def __radd__(self: Series[float], other: int | Sequence[int]) -> Series[float]: ...
1713+
def __radd__(self: Series[int], other: np_ndarray_anyint) -> Series[int]: ...
1714+
@overload
1715+
def __radd__(self: Series[int], other: np_ndarray_float) -> Series[float]: ...
1716+
@overload
1717+
def __radd__(self: Series[int], other: np_ndarray_complex) -> Series[complex]: ...
1718+
@overload
1719+
def __radd__(
1720+
self: Series[float],
1721+
other: int | Sequence[int] | np_ndarray_anyint | np_ndarray_float,
1722+
) -> Series[float]: ...
17141723
@overload
17151724
def __radd__(
17161725
self: Series[float], other: _T_COMPLEX | Sequence[_T_COMPLEX]
17171726
) -> Series[_T_COMPLEX]: ...
17181727
@overload
1728+
def __radd__(self: Series[float], other: np_ndarray_complex) -> Series[complex]: ...
1729+
@overload
17191730
def __radd__(
1720-
self: Series[complex], other: complex | Sequence[complex]
1731+
self: Series[complex],
1732+
other: (
1733+
np_ndarray_anyint
1734+
| np_ndarray_float
1735+
| np_ndarray_complex
1736+
| Sequence[_T_COMPLEX]
1737+
),
17211738
) -> Series[complex]: ...
17221739
@overload
1723-
def __radd__(self, other: S1 | Series[S1]) -> Self: ...
1740+
def __radd__(self, other: S1) -> Self: ...
17241741
# ignore needed for mypy as we want different results based on the arguments
17251742
@overload # type: ignore[override]
17261743
def __rand__( # pyright: ignore[reportOverlappingOverload]
@@ -2608,7 +2625,7 @@ class PeriodSeries(Series[Period]):
26082625
) -> Never: ...
26092626

26102627
class OffsetSeries(Series[BaseOffset]):
2611-
@overload # type: ignore[override]
2628+
@overload
26122629
def __radd__(self, other: Period) -> PeriodSeries: ...
26132630
@overload
26142631
def __radd__( # pyright: ignore[reportIncompatibleMethodOverride]

tests/series/arithmetic/complex/test_add.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
from numpy import typing as npt # noqa: F401
23
import pandas as pd
34
from typing_extensions import assert_type
45

@@ -59,28 +60,12 @@ def test_add_numpy_array() -> None:
5960
check(assert_type(left + f, "pd.Series[complex]"), pd.Series, np.complex128)
6061
check(assert_type(left + c, "pd.Series[complex]"), pd.Series, np.complex128)
6162

62-
# numpy typing gives ndarray instead of `pd.Series[...]` in reality, which we cannot fix
63-
check(
64-
assert_type( # type: ignore[assert-type]
65-
i + left, "pd.Series[complex]" # pyright: ignore[reportAssertTypeFailure]
66-
),
67-
pd.Series,
68-
np.complex128,
69-
)
70-
check(
71-
assert_type( # type: ignore[assert-type]
72-
f + left, "pd.Series[complex]" # pyright: ignore[reportAssertTypeFailure]
73-
),
74-
pd.Series,
75-
np.complex128,
76-
)
77-
check(
78-
assert_type( # type: ignore[assert-type]
79-
c + left, "pd.Series[complex]" # pyright: ignore[reportAssertTypeFailure]
80-
),
81-
pd.Series,
82-
np.complex128,
83-
)
63+
# `numpy` typing gives the corresponding `ndarray`s in the static type
64+
# checking, where our `__radd__` cannot override. At runtime, they return
65+
# `Series`s with the correct element type.
66+
check(assert_type(i + left, "npt.NDArray[np.int64]"), pd.Series, np.complex128)
67+
check(assert_type(f + left, "npt.NDArray[np.float64]"), pd.Series, np.complex128)
68+
check(assert_type(c + left, "npt.NDArray[np.complex128]"), pd.Series, np.complex128)
8469

8570
check(assert_type(left.add(i), "pd.Series[complex]"), pd.Series, np.complex128)
8671
check(assert_type(left.add(f), "pd.Series[complex]"), pd.Series, np.complex128)

tests/series/arithmetic/float/test_add.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
from numpy import typing as npt # noqa: F401
23
import pandas as pd
34
from typing_extensions import assert_type
45

@@ -59,28 +60,12 @@ def test_add_numpy_array() -> None:
5960
check(assert_type(left + f, "pd.Series[float]"), pd.Series, np.float64)
6061
check(assert_type(left + c, "pd.Series[complex]"), pd.Series, np.complex128)
6162

62-
# numpy typing gives ndarray instead of `pd.Series[...]` in reality, which we cannot fix
63-
check(
64-
assert_type( # type: ignore[assert-type]
65-
i + left, "pd.Series[float]" # pyright: ignore[reportAssertTypeFailure]
66-
),
67-
pd.Series,
68-
np.float64,
69-
)
70-
check(
71-
assert_type( # type: ignore[assert-type]
72-
f + left, "pd.Series[float]" # pyright: ignore[reportAssertTypeFailure]
73-
),
74-
pd.Series,
75-
np.float64,
76-
)
77-
check(
78-
assert_type( # type: ignore[assert-type]
79-
c + left, "pd.Series[complex]" # pyright: ignore[reportAssertTypeFailure]
80-
),
81-
pd.Series,
82-
np.complex128,
83-
)
63+
# `numpy` typing gives the corresponding `ndarray`s in the static type
64+
# checking, where our `__radd__` cannot override. At runtime, they return
65+
# `Series`s with the correct element type.
66+
check(assert_type(i + left, "npt.NDArray[np.int64]"), pd.Series, np.float64)
67+
check(assert_type(f + left, "npt.NDArray[np.float64]"), pd.Series, np.float64)
68+
check(assert_type(c + left, "npt.NDArray[np.complex128]"), pd.Series, np.complex128)
8469

8570
check(assert_type(left.add(i), "pd.Series[float]"), pd.Series, np.float64)
8671
check(assert_type(left.add(f), "pd.Series[float]"), pd.Series, np.float64)

tests/series/arithmetic/int/test_add.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
from numpy import typing as npt # noqa: F401
23
import pandas as pd
34
from typing_extensions import assert_type
45

@@ -59,25 +60,13 @@ def test_add_numpy_array() -> None:
5960
check(assert_type(left + f, "pd.Series[float]"), pd.Series, np.float64)
6061
check(assert_type(left + c, "pd.Series[complex]"), pd.Series, np.complex128)
6162

62-
# numpy typing gives ndarray instead of `pd.Series[...]` in reality, which we cannot fix
63+
# `numpy` typing gives the corresponding `ndarray`s in the static type
64+
# checking, where our `__radd__` cannot override. At runtime, they return
65+
# `Series`s with the correct element type.
66+
check(assert_type(i + left, "npt.NDArray[np.int64]"), pd.Series, np.int64)
67+
check(assert_type(f + left, "npt.NDArray[np.float64]"), pd.Series, np.float64)
6368
check(
64-
assert_type( # type: ignore[assert-type]
65-
i + left, "pd.Series[int]" # pyright: ignore[reportAssertTypeFailure]
66-
),
67-
pd.Series,
68-
np.int64,
69-
)
70-
check(
71-
assert_type( # type: ignore[assert-type]
72-
f + left, "pd.Series[float]" # pyright: ignore[reportAssertTypeFailure]
73-
),
74-
pd.Series,
75-
np.float64,
76-
)
77-
check(
78-
assert_type( # type: ignore[assert-type]
79-
c + left, "pd.Series[complex]" # pyright: ignore[reportAssertTypeFailure]
80-
),
69+
assert_type(c + left, "npt.NDArray[np.complex128]"),
8170
pd.Series,
8271
np.complex128,
8372
)

tests/series/arithmetic/test_add.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import numpy as np
2+
from numpy import typing as npt # noqa: F401
3+
from numpy._typing import _32Bit # noqa: F401
24
import pandas as pd
35
from typing_extensions import assert_type
46

@@ -59,24 +61,18 @@ def test_add_numpy_array() -> None:
5961
check(assert_type(left + f, pd.Series), pd.Series)
6062
check(assert_type(left + c, pd.Series), pd.Series)
6163

62-
# numpy typing gives ndarray instead of `pd.Series[...]` in reality, which we cannot fix
64+
# `numpy` typing gives the corresponding `ndarray`s in the static type
65+
# checking, where our `__radd__` cannot override. At runtime, they return
66+
# `Series`s.
67+
# `mypy` thinks the return types are `Any`, which is a bug.
6368
check(
64-
assert_type( # type: ignore[assert-type]
65-
i + left, pd.Series # pyright: ignore[reportAssertTypeFailure]
66-
),
67-
pd.Series,
69+
assert_type(i + left, "npt.NDArray[np.int64]"), pd.Series # type: ignore[assert-type]
6870
)
6971
check(
70-
assert_type( # type: ignore[assert-type]
71-
f + left, pd.Series # pyright: ignore[reportAssertTypeFailure]
72-
),
73-
pd.Series,
72+
assert_type(f + left, "npt.NDArray[np.float64]"), pd.Series # type: ignore[assert-type]
7473
)
7574
check(
76-
assert_type( # type: ignore[assert-type]
77-
c + left, pd.Series # pyright: ignore[reportAssertTypeFailure]
78-
),
79-
pd.Series,
75+
assert_type(c + left, "npt.NDArray[np.complexfloating[_32Bit, _32Bit]]"), pd.Series # type: ignore[assert-type]
8076
)
8177

8278
check(assert_type(left.add(i), pd.Series), pd.Series)

0 commit comments

Comments
 (0)