|
| 1 | +import sys |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from numpy import typing as npt # noqa: F401 |
| 6 | +import pandas as pd |
| 7 | +from typing_extensions import ( |
| 8 | + Never, |
| 9 | + assert_type, |
| 10 | +) |
| 11 | + |
| 12 | +from tests import ( |
| 13 | + TYPE_CHECKING_INVALID_USAGE, |
| 14 | + check, |
| 15 | +) |
| 16 | + |
| 17 | +left = pd.Series(["1", "23", "456"]) # left operand |
| 18 | + |
| 19 | + |
| 20 | +def test_add_py_scalar() -> None: |
| 21 | + """Testpd.Series[str]+ Python native 'scalar's""" |
| 22 | + i = 4 |
| 23 | + r0 = "right" |
| 24 | + |
| 25 | + if TYPE_CHECKING_INVALID_USAGE: |
| 26 | + _0 = left + i # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 27 | + check(assert_type(left + r0, "pd.Series[str]"), pd.Series, str) |
| 28 | + |
| 29 | + if TYPE_CHECKING_INVALID_USAGE: |
| 30 | + _1 = i + left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 31 | + check(assert_type(r0 + left, "pd.Series[str]"), pd.Series, str) |
| 32 | + |
| 33 | + if TYPE_CHECKING_INVALID_USAGE: |
| 34 | + left.add(i) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] |
| 35 | + check(assert_type(left.add(r0), "pd.Series[str]"), pd.Series, str) |
| 36 | + |
| 37 | + if TYPE_CHECKING_INVALID_USAGE: |
| 38 | + left.radd(i) # type: ignore[call-overload] # pyright: ignore[reportArgumentType] |
| 39 | + check(assert_type(left.radd(r0), "pd.Series[str]"), pd.Series, str) |
| 40 | + |
| 41 | + |
| 42 | +def test_add_py_sequence() -> None: |
| 43 | + """Testpd.Series[str]+ Python native sequence""" |
| 44 | + i = [3, 5, 8] |
| 45 | + r0 = ["a", "bc", "def"] |
| 46 | + r1 = tuple(r0) |
| 47 | + |
| 48 | + if TYPE_CHECKING_INVALID_USAGE: |
| 49 | + _0 = left + i # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 50 | + check(assert_type(left + r0, "pd.Series[str]"), pd.Series, str) |
| 51 | + check(assert_type(left + r1, "pd.Series[str]"), pd.Series, str) |
| 52 | + |
| 53 | + if TYPE_CHECKING_INVALID_USAGE: |
| 54 | + _1 = i + left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 55 | + check(assert_type(r0 + left, "pd.Series[str]"), pd.Series, str) |
| 56 | + check(assert_type(r1 + left, "pd.Series[str]"), pd.Series, str) |
| 57 | + |
| 58 | + if TYPE_CHECKING_INVALID_USAGE: |
| 59 | + left.add(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] |
| 60 | + check(assert_type(left.add(r0), "pd.Series[str]"), pd.Series, str) |
| 61 | + check(assert_type(left.add(r1), "pd.Series[str]"), pd.Series, str) |
| 62 | + |
| 63 | + if TYPE_CHECKING_INVALID_USAGE: |
| 64 | + left.radd(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] |
| 65 | + check(assert_type(left.radd(r0), "pd.Series[str]"), pd.Series, str) |
| 66 | + check(assert_type(left.radd(r1), "pd.Series[str]"), pd.Series, str) |
| 67 | + |
| 68 | + |
| 69 | +def test_add_numpy_array() -> None: |
| 70 | + """Testpd.Series[str]+ numpy array""" |
| 71 | + i = np.array([3, 5, 8], np.int64) |
| 72 | + r0 = np.array(["a", "bc", "def"], np.str_) |
| 73 | + |
| 74 | + if TYPE_CHECKING_INVALID_USAGE: |
| 75 | + assert_type(left + i, Never) |
| 76 | + check(assert_type(left + r0, "pd.Series[str]"), pd.Series, str) |
| 77 | + |
| 78 | + # `numpy` typing gives `npt.NDArray[np.int64]` in the static type |
| 79 | + # checking, where our `__radd__` cannot override. At runtime, they return |
| 80 | + # `Series`s. |
| 81 | + if TYPE_CHECKING_INVALID_USAGE: |
| 82 | + assert_type(i + left, "npt.NDArray[np.int64]") |
| 83 | + if sys.version_info >= (3, 11): |
| 84 | + # `numpy` typing gives `npt.NDArray[np.int64]` in the static type |
| 85 | + # checking, where our `__radd__` cannot override. At runtime, they return |
| 86 | + # `Series`s. |
| 87 | + check(assert_type(r0 + left, "npt.NDArray[np.str_]"), pd.Series, str) |
| 88 | + else: |
| 89 | + # Python 3.10 uses NumPy 2.2.6, and it has for r0 ndarray[tuple[int,...], dtype[str_]] |
| 90 | + # Python 3.11+ uses NumPy 2.3.2, and it has for r0 ndarray[tuple[Any,...,dtype[str_]] |
| 91 | + # https://github.com/pandas-dev/pandas-stubs/pull/1274#discussion_r2291498975 |
| 92 | + check(assert_type(r0 + left, Any), pd.Series, str) |
| 93 | + |
| 94 | + if TYPE_CHECKING_INVALID_USAGE: |
| 95 | + left.add(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] |
| 96 | + check(assert_type(left.add(r0), "pd.Series[str]"), pd.Series, str) |
| 97 | + |
| 98 | + if TYPE_CHECKING_INVALID_USAGE: |
| 99 | + left.radd(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] |
| 100 | + check(assert_type(left.radd(r0), "pd.Series[str]"), pd.Series, str) |
| 101 | + |
| 102 | + |
| 103 | +def test_add_pd_series() -> None: |
| 104 | + """Testpd.Series[str]+ pandas series""" |
| 105 | + i = pd.Series([3, 5, 8]) |
| 106 | + r0 = pd.Series(["a", "bc", "def"]) |
| 107 | + |
| 108 | + if TYPE_CHECKING_INVALID_USAGE: |
| 109 | + _0 = left + i # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 110 | + check(assert_type(left + r0, "pd.Series[str]"), pd.Series, str) |
| 111 | + |
| 112 | + if TYPE_CHECKING_INVALID_USAGE: |
| 113 | + _1 = i + left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 114 | + check(assert_type(r0 + left, "pd.Series[str]"), pd.Series, str) |
| 115 | + |
| 116 | + if TYPE_CHECKING_INVALID_USAGE: |
| 117 | + left.add(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue] |
| 118 | + check(assert_type(left.add(r0), "pd.Series[str]"), pd.Series, str) |
| 119 | + |
| 120 | + if TYPE_CHECKING_INVALID_USAGE: |
| 121 | + left.radd(i) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] |
| 122 | + check(assert_type(left.radd(r0), "pd.Series[str]"), pd.Series, str) |
0 commit comments