11import datetime as dt
2- from typing import Any
2+ from typing import Literal as L
3+
4+ from typing_extensions import assert_type
35
46import numpy as np
57import numpy .typing as npt
6- from numpy ._typing import _32Bit , _64Bit
8+ from numpy ._typing import _64Bit
79
8- from typing_extensions import assert_type
10+ f8 : np .float64
11+ i8 : np .int64
12+ u8 : np .uint64
913
10- f8 = np .float64 ()
11- i8 = np .int64 ()
12- u8 = np .uint64 ()
14+ f4 : np .float32
15+ i4 : np .int32
16+ u4 : np .uint32
1317
14- f4 = np .float32 ()
15- i4 = np .int32 ()
16- u4 = np .uint32 ()
18+ m : np .timedelta64
19+ m_nat : np .timedelta64 [None ]
20+ m_int0 : np .timedelta64 [L [0 ]]
21+ m_int : np .timedelta64 [int ]
22+ m_td : np .timedelta64 [dt .timedelta ]
1723
18- td = np .timedelta64 (0 , "D" )
19- b_ = np .bool ()
24+ b_ : np .bool
2025
21- b = bool ()
22- f = float ()
23- i = int ()
26+ b : bool
27+ i : int
28+ f : float
2429
2530AR_b : npt .NDArray [np .bool ]
2631AR_m : npt .NDArray [np .timedelta64 ]
2732
2833# Time structures
2934
30- assert_type (td % td , np .timedelta64 [dt .timedelta ])
31- assert_type (AR_m % td , npt .NDArray [np .timedelta64 ])
32- assert_type (td % AR_m , npt .NDArray [np .timedelta64 ])
33-
34- assert_type (divmod (td , td ), tuple [np .int64 , np .timedelta64 ])
35- assert_type (divmod (AR_m , td ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .timedelta64 ]])
36- assert_type (divmod (td , AR_m ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .timedelta64 ]])
35+ assert_type (m % m , np .timedelta64 )
36+ assert_type (m % m_nat , np .timedelta64 [None ])
37+ assert_type (m % m_int0 , np .timedelta64 [None ])
38+ assert_type (m % m_int , np .timedelta64 [int | None ])
39+ assert_type (m_nat % m , np .timedelta64 [None ])
40+ assert_type (m_int % m_nat , np .timedelta64 [None ])
41+ assert_type (m_int % m_int0 , np .timedelta64 [None ])
42+ assert_type (m_int % m_int , np .timedelta64 [int | None ])
43+ assert_type (m_int % m_td , np .timedelta64 [int | None ])
44+ assert_type (m_td % m_nat , np .timedelta64 [None ])
45+ assert_type (m_td % m_int0 , np .timedelta64 [None ])
46+ assert_type (m_td % m_int , np .timedelta64 [int | None ])
47+ assert_type (m_td % m_td , np .timedelta64 [dt .timedelta | None ])
48+
49+ assert_type (AR_m % m , npt .NDArray [np .timedelta64 ])
50+ assert_type (m % AR_m , npt .NDArray [np .timedelta64 ])
51+
52+ assert_type (divmod (m , m ), tuple [np .int64 , np .timedelta64 ])
53+ assert_type (divmod (m , m_nat ), tuple [np .int64 , np .timedelta64 [None ]])
54+ assert_type (divmod (m , m_int0 ), tuple [np .int64 , np .timedelta64 [None ]])
55+ # workarounds for https://github.com/microsoft/pyright/issues/9663
56+ assert_type (m .__divmod__ (m_int ), tuple [np .int64 , np .timedelta64 [int | None ]])
57+ assert_type (divmod (m_nat , m ), tuple [np .int64 , np .timedelta64 [None ]])
58+ assert_type (divmod (m_int , m_nat ), tuple [np .int64 , np .timedelta64 [None ]])
59+ assert_type (divmod (m_int , m_int0 ), tuple [np .int64 , np .timedelta64 [None ]])
60+ assert_type (divmod (m_int , m_int ), tuple [np .int64 , np .timedelta64 [int | None ]])
61+ assert_type (divmod (m_int , m_td ), tuple [np .int64 , np .timedelta64 [int | None ]])
62+ assert_type (divmod (m_td , m_nat ), tuple [np .int64 , np .timedelta64 [None ]])
63+ assert_type (divmod (m_td , m_int0 ), tuple [np .int64 , np .timedelta64 [None ]])
64+ assert_type (divmod (m_td , m_int ), tuple [np .int64 , np .timedelta64 [int | None ]])
65+ assert_type (divmod (m_td , m_td ), tuple [np .int64 , np .timedelta64 [dt .timedelta | None ]])
66+
67+ assert_type (divmod (AR_m , m ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .timedelta64 ]])
68+ assert_type (divmod (m , AR_m ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .timedelta64 ]])
3769
3870# Bool
3971
@@ -47,11 +79,12 @@ assert_type(b_ % f8, np.float64)
4779assert_type (b_ % AR_b , npt .NDArray [np .int8 ])
4880
4981assert_type (divmod (b_ , b ), tuple [np .int8 , np .int8 ])
50- assert_type (divmod (b_ , i ), tuple [np .int_ , np .int_ ])
51- assert_type (divmod (b_ , f ), tuple [np .float64 , np .float64 ])
5282assert_type (divmod (b_ , b_ ), tuple [np .int8 , np .int8 ])
53- assert_type (divmod (b_ , i8 ), tuple [np .int64 , np .int64 ])
54- assert_type (divmod (b_ , u8 ), tuple [np .uint64 , np .uint64 ])
83+ # workarounds for https://github.com/microsoft/pyright/issues/9663
84+ assert_type (b_ .__divmod__ (i ), tuple [np .int_ , np .int_ ])
85+ assert_type (b_ .__divmod__ (f ), tuple [np .float64 , np .float64 ])
86+ assert_type (b_ .__divmod__ (i8 ), tuple [np .int64 , np .int64 ])
87+ assert_type (b_ .__divmod__ (u8 ), tuple [np .uint64 , np .uint64 ])
5588assert_type (divmod (b_ , f8 ), tuple [np .float64 , np .float64 ])
5689assert_type (divmod (b_ , AR_b ), tuple [npt .NDArray [np .int8 ], npt .NDArray [np .int8 ]])
5790
@@ -77,26 +110,27 @@ assert_type(divmod(AR_b, b_), tuple[npt.NDArray[np.int8], npt.NDArray[np.int8]])
77110
78111assert_type (i8 % b , np .int64 )
79112assert_type (i8 % i8 , np .int64 )
80- assert_type (i8 % f , np .floating [_64Bit ])
81- assert_type (i8 % f8 , np .floating [_64Bit ])
113+ assert_type (i8 % f , np .float64 | np . floating [_64Bit ])
114+ assert_type (i8 % f8 , np .float64 | np . floating [_64Bit ])
82115assert_type (i4 % i8 , np .int64 | np .int32 )
83116assert_type (i4 % f8 , np .float64 | np .float32 )
84117assert_type (i4 % i4 , np .int32 )
85118assert_type (i4 % f4 , np .float32 )
86119assert_type (i8 % AR_b , npt .NDArray [np .int64 ])
87120
88- assert_type (divmod (i8 , b ), tuple [np .signedinteger [_64Bit ], np .signedinteger [_64Bit ]])
89- assert_type (divmod (i8 , f ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]])
90- assert_type (divmod (i8 , i8 ), tuple [np .signedinteger [_64Bit ], np .signedinteger [_64Bit ]])
91- assert_type (divmod (i8 , f8 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]])
92- assert_type (divmod (i8 , i4 ), tuple [np .signedinteger [_64Bit ], np .signedinteger [_64Bit ]] | tuple [np .signedinteger [_32Bit ], np .signedinteger [_32Bit ]])
93- assert_type (divmod (i8 , f4 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]] | tuple [np .floating [_32Bit ], np .floating [_32Bit ]])
94- assert_type (divmod (i4 , i4 ), tuple [np .signedinteger [_32Bit ], np .signedinteger [_32Bit ]])
95- assert_type (divmod (i4 , f4 ), tuple [np .floating [_32Bit ], np .floating [_32Bit ]])
121+ assert_type (divmod (i8 , b ), tuple [np .int64 , np .int64 ])
122+ assert_type (divmod (i8 , i4 ), tuple [np .int64 , np .int64 ] | tuple [np .int32 , np .int32 ])
123+ assert_type (divmod (i8 , i8 ), tuple [np .int64 , np .int64 ])
124+ # workarounds for https://github.com/microsoft/pyright/issues/9663
125+ assert_type (i8 .__divmod__ (f ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]] | tuple [np .float64 , np .float64 ])
126+ assert_type (i8 .__divmod__ (f8 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]] | tuple [np .float64 , np .float64 ])
127+ assert_type (divmod (i8 , f4 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]] | tuple [np .float32 , np .float32 ])
128+ assert_type (divmod (i4 , i4 ), tuple [np .int32 , np .int32 ])
129+ assert_type (divmod (i4 , f4 ), tuple [np .float32 , np .float32 ])
96130assert_type (divmod (i8 , AR_b ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .int64 ]])
97131
98- assert_type (b % i8 , np .signedinteger [ _64Bit ] )
99- assert_type (f % i8 , np .floating [_64Bit ])
132+ assert_type (b % i8 , np .int64 )
133+ assert_type (f % i8 , np .float64 | np . floating [_64Bit ])
100134assert_type (i8 % i8 , np .int64 )
101135assert_type (f8 % i8 , np .float64 )
102136assert_type (i8 % i4 , np .int64 | np .int32 )
@@ -105,21 +139,22 @@ assert_type(i4 % i4, np.int32)
105139assert_type (f4 % i4 , np .float32 )
106140assert_type (AR_b % i8 , npt .NDArray [np .int64 ])
107141
108- assert_type (divmod (b , i8 ), tuple [np .signedinteger [ _64Bit ] , np .signedinteger [ _64Bit ] ])
109- assert_type (divmod (f , i8 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]])
142+ assert_type (divmod (b , i8 ), tuple [np .int64 , np .int64 ])
143+ assert_type (divmod (f , i8 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]] | tuple [ np . float64 , np . float64 ] )
110144assert_type (divmod (i8 , i8 ), tuple [np .int64 , np .int64 ])
111145assert_type (divmod (f8 , i8 ), tuple [np .float64 , np .float64 ])
112- assert_type (divmod (i4 , i8 ), tuple [np .signedinteger [_64Bit ], np .signedinteger [_64Bit ]] | tuple [np .signedinteger [_32Bit ], np .signedinteger [_32Bit ]])
113- assert_type (divmod (f4 , i8 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]] | tuple [np .floating [_32Bit ], np .floating [_32Bit ]])
114- assert_type (divmod (i4 , i4 ), tuple [np .signedinteger [_32Bit ], np .signedinteger [_32Bit ]])
115- assert_type (divmod (f4 , i4 ), tuple [np .floating [_32Bit ], np .floating [_32Bit ]])
116- assert_type (divmod (AR_b , i8 ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .int64 ]])
146+ assert_type (divmod (i4 , i8 ), tuple [np .int64 , np .int64 ] | tuple [np .int32 , np .int32 ])
147+ assert_type (divmod (i4 , i4 ), tuple [np .int32 , np .int32 ])
148+ # workarounds for https://github.com/microsoft/pyright/issues/9663
149+ assert_type (f4 .__divmod__ (i8 ), tuple [np .floating [_64Bit ], np .floating [_64Bit ]] | tuple [np .float32 , np .float32 ])
150+ assert_type (f4 .__divmod__ (i4 ), tuple [np .float32 , np .float32 ])
151+ assert_type (AR_b .__divmod__ (i8 ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .int64 ]])
117152
118153# float
119154
120155assert_type (f8 % b , np .float64 )
121156assert_type (f8 % f , np .float64 )
122- assert_type (i8 % f4 , np .floating [_64Bit ] | np .floating [ _32Bit ] )
157+ assert_type (i8 % f4 , np .floating [_64Bit ] | np .float32 )
123158assert_type (f4 % f4 , np .float32 )
124159assert_type (f8 % AR_b , npt .NDArray [np .float64 ])
125160
@@ -131,15 +166,16 @@ assert_type(divmod(f4, f4), tuple[np.float32, np.float32])
131166assert_type (divmod (f8 , AR_b ), tuple [npt .NDArray [np .float64 ], npt .NDArray [np .float64 ]])
132167
133168assert_type (b % f8 , np .float64 )
134- assert_type (f % f8 , np .float64 )
169+ assert_type (f % f8 , np .float64 ) # pyright: ignore[reportAssertTypeFailure] # pyright incorrectly infers `builtins.float`
135170assert_type (f8 % f8 , np .float64 )
136171assert_type (f8 % f8 , np .float64 )
137172assert_type (f4 % f4 , np .float32 )
138173assert_type (AR_b % f8 , npt .NDArray [np .float64 ])
139174
140175assert_type (divmod (b , f8 ), tuple [np .float64 , np .float64 ])
141- assert_type (divmod (f , f8 ), tuple [np .float64 , np .float64 ])
142176assert_type (divmod (f8 , f8 ), tuple [np .float64 , np .float64 ])
143- assert_type (divmod (f4 , f8 ), tuple [np .float64 , np .float64 ] | tuple [np .float32 , np .float32 ])
144177assert_type (divmod (f4 , f4 ), tuple [np .float32 , np .float32 ])
145- assert_type (divmod (AR_b , f8 ), tuple [npt .NDArray [np .float64 ], npt .NDArray [np .float64 ]])
178+ # workarounds for https://github.com/microsoft/pyright/issues/9663
179+ assert_type (f8 .__rdivmod__ (f ), tuple [np .float64 , np .float64 ])
180+ assert_type (f8 .__rdivmod__ (f4 ), tuple [np .float64 , np .float64 ])
181+ assert_type (AR_b .__divmod__ (f8 ), tuple [npt .NDArray [np .float64 ], npt .NDArray [np .float64 ]])
0 commit comments