1+ from typing import Never
2+
13import numpy as np
24from numpy import typing as npt # noqa: F401
35import pandas as pd
46from typing_extensions import assert_type
57
6- from tests import check
8+ from tests import (
9+ TYPE_CHECKING_INVALID_USAGE ,
10+ check ,
11+ )
712
813left = pd .Series ([True , False , True ]) # left operand
914
1015
1116def test_truediv_py_scalar () -> None :
1217 """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
1419
20+ if TYPE_CHECKING_INVALID_USAGE :
21+ assert_type (left / b , Never )
1522 check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
1623 check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
1724 check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
1825
26+ if TYPE_CHECKING_INVALID_USAGE :
27+ assert_type (b / left , Never )
1928 check (assert_type (i / left , "pd.Series[float]" ), pd .Series , np .floating )
2029 check (assert_type (f / left , "pd.Series[float]" ), pd .Series , np .floating )
2130 check (assert_type (c / left , "pd.Series[complex]" ), pd .Series , np .complexfloating )
2231
32+ if TYPE_CHECKING_INVALID_USAGE :
33+ assert_type (left .truediv (b ), Never )
2334 check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
2435 check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
2536 check (
@@ -28,10 +39,14 @@ def test_truediv_py_scalar() -> None:
2839 np .complexfloating ,
2940 )
3041
42+ if TYPE_CHECKING_INVALID_USAGE :
43+ assert_type (left .div (b ), Never )
3144 check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
3245 check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
3346 check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
3447
48+ if TYPE_CHECKING_INVALID_USAGE :
49+ assert_type (left .rtruediv (b ), Never )
3550 check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
3651 check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
3752 check (
@@ -40,6 +55,8 @@ def test_truediv_py_scalar() -> None:
4055 np .complexfloating ,
4156 )
4257
58+ if TYPE_CHECKING_INVALID_USAGE :
59+ assert_type (left .rdiv (b ), Never )
4360 check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
4461 check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
4562 check (
@@ -49,16 +66,19 @@ def test_truediv_py_scalar() -> None:
4966
5067def test_truediv_py_sequence () -> None :
5168 """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 ]
5370
71+ check (assert_type (left / b , "pd.Series[float]" ), pd .Series , np .floating )
5472 check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
5573 check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
5674 check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
5775
76+ check (assert_type (b / left , "pd.Series[float]" ), pd .Series , np .floating )
5877 check (assert_type (i / left , "pd.Series[float]" ), pd .Series , np .floating )
5978 check (assert_type (f / left , "pd.Series[float]" ), pd .Series , np .floating )
6079 check (assert_type (c / left , "pd.Series[complex]" ), pd .Series , np .complexfloating )
6180
81+ check (assert_type (left .truediv (b ), "pd.Series[float]" ), pd .Series , np .floating )
6282 check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
6383 check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
6484 check (
@@ -67,10 +87,12 @@ def test_truediv_py_sequence() -> None:
6787 np .complexfloating ,
6888 )
6989
90+ check (assert_type (left .div (b ), "pd.Series[float]" ), pd .Series , np .floating )
7091 check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
7192 check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
7293 check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
7394
95+ check (assert_type (left .rtruediv (b ), "pd.Series[float]" ), pd .Series , np .floating )
7496 check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
7597 check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
7698 check (
@@ -79,6 +101,7 @@ def test_truediv_py_sequence() -> None:
79101 np .complexfloating ,
80102 )
81103
104+ check (assert_type (left .rdiv (b ), "pd.Series[float]" ), pd .Series , np .floating )
82105 check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
83106 check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
84107 check (
@@ -88,17 +111,22 @@ def test_truediv_py_sequence() -> None:
88111
89112def test_truediv_numpy_array () -> None :
90113 """Test pd.Series[bool] / numpy array"""
114+ b = np .array ([True , False , True ], np .bool_ )
91115 i = np .array ([2 , 3 , 5 ], np .int64 )
92116 f = np .array ([1.0 , 2.0 , 3.0 ], np .float64 )
93117 c = np .array ([1.1j , 2.2j , 4.1j ], np .complex128 )
94118
119+ if TYPE_CHECKING_INVALID_USAGE :
120+ assert_type (left / b , Never )
95121 check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
96122 check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
97123 check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
98124
99125 # `numpy` typing gives the corresponding `ndarray`s in the static type
100126 # checking, where our `__rtruediv__` cannot override. At runtime, they return
101127 # `Series`s with the correct element type.
128+ if TYPE_CHECKING_INVALID_USAGE :
129+ assert_type (b / left , "npt.NDArray[np.float64]" )
102130 check (assert_type (i / left , "npt.NDArray[np.float64]" ), pd .Series , np .floating )
103131 check (assert_type (f / left , "npt.NDArray[np.float64]" ), pd .Series , np .floating )
104132 check (
@@ -107,6 +135,8 @@ def test_truediv_numpy_array() -> None:
107135 np .complexfloating ,
108136 )
109137
138+ if TYPE_CHECKING_INVALID_USAGE :
139+ assert_type (left .truediv (b ), Never )
110140 check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
111141 check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
112142 check (
@@ -115,10 +145,14 @@ def test_truediv_numpy_array() -> None:
115145 np .complexfloating ,
116146 )
117147
148+ if TYPE_CHECKING_INVALID_USAGE :
149+ assert_type (left .div (b ), Never )
118150 check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
119151 check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
120152 check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
121153
154+ if TYPE_CHECKING_INVALID_USAGE :
155+ assert_type (left .rtruediv (b ), Never )
122156 check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
123157 check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
124158 check (
@@ -127,6 +161,8 @@ def test_truediv_numpy_array() -> None:
127161 np .complexfloating ,
128162 )
129163
164+ if TYPE_CHECKING_INVALID_USAGE :
165+ assert_type (left .rdiv (b ), Never )
130166 check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
131167 check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
132168 check (
@@ -136,18 +172,25 @@ def test_truediv_numpy_array() -> None:
136172
137173def test_truediv_pd_series () -> None :
138174 """Test pd.Series[bool] / pandas series"""
175+ b = pd .Series ([True , False , True ])
139176 i = pd .Series ([2 , 3 , 5 ])
140177 f = pd .Series ([1.0 , 2.0 , 3.0 ])
141178 c = pd .Series ([1.1j , 2.2j , 4.1j ])
142179
180+ if TYPE_CHECKING_INVALID_USAGE :
181+ assert_type (left / b , Never )
143182 check (assert_type (left / i , "pd.Series[float]" ), pd .Series , np .floating )
144183 check (assert_type (left / f , "pd.Series[float]" ), pd .Series , np .floating )
145184 check (assert_type (left / c , "pd.Series[complex]" ), pd .Series , np .complexfloating )
146185
186+ if TYPE_CHECKING_INVALID_USAGE :
187+ assert_type (b / left , Never )
147188 check (assert_type (i / left , "pd.Series[float]" ), pd .Series , np .floating )
148189 check (assert_type (f / left , "pd.Series[float]" ), pd .Series , np .floating )
149190 check (assert_type (c / left , "pd.Series[complex]" ), pd .Series , np .complexfloating )
150191
192+ if TYPE_CHECKING_INVALID_USAGE :
193+ assert_type (left .truediv (b ), Never )
151194 check (assert_type (left .truediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
152195 check (assert_type (left .truediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
153196 check (
@@ -156,10 +199,14 @@ def test_truediv_pd_series() -> None:
156199 np .complexfloating ,
157200 )
158201
202+ if TYPE_CHECKING_INVALID_USAGE :
203+ assert_type (left .div (b ), Never )
159204 check (assert_type (left .div (i ), "pd.Series[float]" ), pd .Series , np .floating )
160205 check (assert_type (left .div (f ), "pd.Series[float]" ), pd .Series , np .floating )
161206 check (assert_type (left .div (c ), "pd.Series[complex]" ), pd .Series , np .complexfloating )
162207
208+ if TYPE_CHECKING_INVALID_USAGE :
209+ assert_type (left .rtruediv (b ), Never )
163210 check (assert_type (left .rtruediv (i ), "pd.Series[float]" ), pd .Series , np .floating )
164211 check (assert_type (left .rtruediv (f ), "pd.Series[float]" ), pd .Series , np .floating )
165212 check (
@@ -168,6 +215,8 @@ def test_truediv_pd_series() -> None:
168215 np .complexfloating ,
169216 )
170217
218+ if TYPE_CHECKING_INVALID_USAGE :
219+ assert_type (left .rdiv (b ), Never )
171220 check (assert_type (left .rdiv (i ), "pd.Series[float]" ), pd .Series , np .floating )
172221 check (assert_type (left .rdiv (f ), "pd.Series[float]" ), pd .Series , np .floating )
173222 check (
0 commit comments