1
+ from typing import Never
2
+
1
3
import numpy as np
2
4
from numpy import typing as npt # noqa: F401
3
5
import pandas as pd
4
6
from typing_extensions import assert_type
5
7
6
- from tests import check
8
+ from tests import (
9
+ TYPE_CHECKING_INVALID_USAGE ,
10
+ check ,
11
+ )
7
12
8
13
left = pd .Series ([True , False , True ]) # left operand
9
14
10
15
11
16
def test_truediv_py_scalar () -> None :
12
17
"""Test pd.Series[bool] / Python native scalars"""
13
- i , f , c = 1 , 1.0 , 1j
18
+ b , i , f , c = True , 1 , 1.0 , 1j
14
19
20
+ if TYPE_CHECKING_INVALID_USAGE :
21
+ assert_type (left / b , Never )
15
22
check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
16
23
check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
17
24
check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
18
25
26
+ if TYPE_CHECKING_INVALID_USAGE :
27
+ assert_type (b / left , Never )
19
28
check (assert_type (i / left , "pd.Series[float]" ), pd .Series , np .floating )
20
29
check (assert_type (f / left , "pd.Series[float]" ), pd .Series , np .floating )
21
30
check (assert_type (c / left , "pd.Series[complex]" ), pd .Series , np .complexfloating )
22
31
32
+ if TYPE_CHECKING_INVALID_USAGE :
33
+ assert_type (left .truediv (b ), Never )
23
34
check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
24
35
check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
25
36
check (
@@ -28,10 +39,14 @@ def test_truediv_py_scalar() -> None:
28
39
np .complexfloating ,
29
40
)
30
41
42
+ if TYPE_CHECKING_INVALID_USAGE :
43
+ assert_type (left .div (b ), Never )
31
44
check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
32
45
check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
33
46
check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
34
47
48
+ if TYPE_CHECKING_INVALID_USAGE :
49
+ assert_type (left .rtruediv (b ), Never )
35
50
check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
36
51
check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
37
52
check (
@@ -40,6 +55,8 @@ def test_truediv_py_scalar() -> None:
40
55
np .complexfloating ,
41
56
)
42
57
58
+ if TYPE_CHECKING_INVALID_USAGE :
59
+ assert_type (left .rdiv (b ), Never )
43
60
check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
44
61
check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
45
62
check (
@@ -49,16 +66,19 @@ def test_truediv_py_scalar() -> None:
49
66
50
67
def test_truediv_py_sequence () -> None :
51
68
"""Test pd.Series[bool] / Python native sequence"""
52
- i , f , c = [2 , 3 , 5 ], [1.0 , 2.0 , 3.0 ], [1j , 1j , 4j ]
69
+ b , i , f , c = [ True , False , True ], [2 , 3 , 5 ], [1.0 , 2.0 , 3.0 ], [1j , 1j , 4j ]
53
70
71
+ check (assert_type (left / b , "pd.Series[float]" ), pd .Series , np .floating )
54
72
check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
55
73
check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
56
74
check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
57
75
76
+ check (assert_type (b / left , "pd.Series[float]" ), pd .Series , np .floating )
58
77
check (assert_type (i / left , "pd.Series[float]" ), pd .Series , np .floating )
59
78
check (assert_type (f / left , "pd.Series[float]" ), pd .Series , np .floating )
60
79
check (assert_type (c / left , "pd.Series[complex]" ), pd .Series , np .complexfloating )
61
80
81
+ check (assert_type (left .truediv (b ), "pd.Series[float]" ), pd .Series , np .floating )
62
82
check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
63
83
check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
64
84
check (
@@ -67,10 +87,12 @@ def test_truediv_py_sequence() -> None:
67
87
np .complexfloating ,
68
88
)
69
89
90
+ check (assert_type (left .div (b ), "pd.Series[float]" ), pd .Series , np .floating )
70
91
check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
71
92
check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
72
93
check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
73
94
95
+ check (assert_type (left .rtruediv (b ), "pd.Series[float]" ), pd .Series , np .floating )
74
96
check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
75
97
check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
76
98
check (
@@ -79,6 +101,7 @@ def test_truediv_py_sequence() -> None:
79
101
np .complexfloating ,
80
102
)
81
103
104
+ check (assert_type (left .rdiv (b ), "pd.Series[float]" ), pd .Series , np .floating )
82
105
check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
83
106
check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
84
107
check (
@@ -88,17 +111,22 @@ def test_truediv_py_sequence() -> None:
88
111
89
112
def test_truediv_numpy_array () -> None :
90
113
"""Test pd.Series[bool] / numpy array"""
114
+ b = np .array ([True , False , True ], np .bool_ )
91
115
i = np .array ([2 , 3 , 5 ], np .int64 )
92
116
f = np .array ([1.0 , 2.0 , 3.0 ], np .float64 )
93
117
c = np .array ([1.1j , 2.2j , 4.1j ], np .complex128 )
94
118
119
+ if TYPE_CHECKING_INVALID_USAGE :
120
+ assert_type (left / b , Never )
95
121
check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
96
122
check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
97
123
check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
98
124
99
125
# `numpy` typing gives the corresponding `ndarray`s in the static type
100
126
# checking, where our `__rtruediv__` cannot override. At runtime, they return
101
127
# `Series`s with the correct element type.
128
+ if TYPE_CHECKING_INVALID_USAGE :
129
+ assert_type (b / left , "npt.NDArray[np.float64]" )
102
130
check (assert_type (i / left , "npt.NDArray[np.float64]" ), pd .Series , np .floating )
103
131
check (assert_type (f / left , "npt.NDArray[np.float64]" ), pd .Series , np .floating )
104
132
check (
@@ -107,6 +135,8 @@ def test_truediv_numpy_array() -> None:
107
135
np .complexfloating ,
108
136
)
109
137
138
+ if TYPE_CHECKING_INVALID_USAGE :
139
+ assert_type (left .truediv (b ), Never )
110
140
check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
111
141
check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
112
142
check (
@@ -115,10 +145,14 @@ def test_truediv_numpy_array() -> None:
115
145
np .complexfloating ,
116
146
)
117
147
148
+ if TYPE_CHECKING_INVALID_USAGE :
149
+ assert_type (left .div (b ), Never )
118
150
check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
119
151
check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
120
152
check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
121
153
154
+ if TYPE_CHECKING_INVALID_USAGE :
155
+ assert_type (left .rtruediv (b ), Never )
122
156
check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
123
157
check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
124
158
check (
@@ -127,6 +161,8 @@ def test_truediv_numpy_array() -> None:
127
161
np .complexfloating ,
128
162
)
129
163
164
+ if TYPE_CHECKING_INVALID_USAGE :
165
+ assert_type (left .rdiv (b ), Never )
130
166
check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
131
167
check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
132
168
check (
@@ -136,18 +172,25 @@ def test_truediv_numpy_array() -> None:
136
172
137
173
def test_truediv_pd_series () -> None :
138
174
"""Test pd.Series[bool] / pandas series"""
175
+ b = pd .Series ([True , False , True ])
139
176
i = pd .Series ([2 , 3 , 5 ])
140
177
f = pd .Series ([1.0 , 2.0 , 3.0 ])
141
178
c = pd .Series ([1.1j , 2.2j , 4.1j ])
142
179
180
+ if TYPE_CHECKING_INVALID_USAGE :
181
+ assert_type (left / b , Never )
143
182
check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
144
183
check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
145
184
check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
146
185
186
+ if TYPE_CHECKING_INVALID_USAGE :
187
+ assert_type (b / left , Never )
147
188
check (assert_type (i / left , "pd.Series[float]" ), pd .Series , np .floating )
148
189
check (assert_type (f / left , "pd.Series[float]" ), pd .Series , np .floating )
149
190
check (assert_type (c / left , "pd.Series[complex]" ), pd .Series , np .complexfloating )
150
191
192
+ if TYPE_CHECKING_INVALID_USAGE :
193
+ assert_type (left .truediv (b ), Never )
151
194
check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
152
195
check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
153
196
check (
@@ -156,10 +199,14 @@ def test_truediv_pd_series() -> None:
156
199
np .complexfloating ,
157
200
)
158
201
202
+ if TYPE_CHECKING_INVALID_USAGE :
203
+ assert_type (left .div (b ), Never )
159
204
check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
160
205
check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
161
206
check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
162
207
208
+ if TYPE_CHECKING_INVALID_USAGE :
209
+ assert_type (left .rtruediv (b ), Never )
163
210
check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
164
211
check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
165
212
check (
@@ -168,6 +215,8 @@ def test_truediv_pd_series() -> None:
168
215
np .complexfloating ,
169
216
)
170
217
218
+ if TYPE_CHECKING_INVALID_USAGE :
219
+ assert_type (left .rdiv (b ), Never )
171
220
check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
172
221
check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
173
222
check (
0 commit comments