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