1
1
import datetime as dt
2
- from typing import Any
2
+ from typing import Literal as L
3
+
4
+ from typing_extensions import assert_type
3
5
4
6
import numpy as np
5
7
import numpy .typing as npt
6
- from numpy ._typing import _32Bit , _64Bit
8
+ from numpy ._typing import _64Bit
7
9
8
- from typing_extensions import assert_type
10
+ f8 : np .float64
11
+ i8 : np .int64
12
+ u8 : np .uint64
9
13
10
- f8 = np .float64 ()
11
- i8 = np .int64 ()
12
- u8 = np .uint64 ()
14
+ f4 : np .float32
15
+ i4 : np .int32
16
+ u4 : np .uint32
13
17
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 ]
17
23
18
- td = np .timedelta64 (0 , "D" )
19
- b_ = np .bool ()
24
+ b_ : np .bool
20
25
21
- b = bool ()
22
- f = float ()
23
- i = int ()
26
+ b : bool
27
+ i : int
28
+ f : float
24
29
25
30
AR_b : npt .NDArray [np .bool ]
26
31
AR_m : npt .NDArray [np .timedelta64 ]
27
32
28
33
# Time structures
29
34
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 ]])
37
69
38
70
# Bool
39
71
@@ -47,11 +79,12 @@ assert_type(b_ % f8, np.float64)
47
79
assert_type (b_ % AR_b , npt .NDArray [np .int8 ])
48
80
49
81
assert_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 ])
52
82
assert_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 ])
55
88
assert_type (divmod (b_ , f8 ), tuple [np .float64 , np .float64 ])
56
89
assert_type (divmod (b_ , AR_b ), tuple [npt .NDArray [np .int8 ], npt .NDArray [np .int8 ]])
57
90
@@ -77,26 +110,27 @@ assert_type(divmod(AR_b, b_), tuple[npt.NDArray[np.int8], npt.NDArray[np.int8]])
77
110
78
111
assert_type (i8 % b , np .int64 )
79
112
assert_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 ])
82
115
assert_type (i4 % i8 , np .int64 | np .int32 )
83
116
assert_type (i4 % f8 , np .float64 | np .float32 )
84
117
assert_type (i4 % i4 , np .int32 )
85
118
assert_type (i4 % f4 , np .float32 )
86
119
assert_type (i8 % AR_b , npt .NDArray [np .int64 ])
87
120
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 ])
96
130
assert_type (divmod (i8 , AR_b ), tuple [npt .NDArray [np .int64 ], npt .NDArray [np .int64 ]])
97
131
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 ])
100
134
assert_type (i8 % i8 , np .int64 )
101
135
assert_type (f8 % i8 , np .float64 )
102
136
assert_type (i8 % i4 , np .int64 | np .int32 )
@@ -105,21 +139,22 @@ assert_type(i4 % i4, np.int32)
105
139
assert_type (f4 % i4 , np .float32 )
106
140
assert_type (AR_b % i8 , npt .NDArray [np .int64 ])
107
141
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 ] )
110
144
assert_type (divmod (i8 , i8 ), tuple [np .int64 , np .int64 ])
111
145
assert_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 ]])
117
152
118
153
# float
119
154
120
155
assert_type (f8 % b , np .float64 )
121
156
assert_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 )
123
158
assert_type (f4 % f4 , np .float32 )
124
159
assert_type (f8 % AR_b , npt .NDArray [np .float64 ])
125
160
@@ -131,15 +166,16 @@ assert_type(divmod(f4, f4), tuple[np.float32, np.float32])
131
166
assert_type (divmod (f8 , AR_b ), tuple [npt .NDArray [np .float64 ], npt .NDArray [np .float64 ]])
132
167
133
168
assert_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`
135
170
assert_type (f8 % f8 , np .float64 )
136
171
assert_type (f8 % f8 , np .float64 )
137
172
assert_type (f4 % f4 , np .float32 )
138
173
assert_type (AR_b % f8 , npt .NDArray [np .float64 ])
139
174
140
175
assert_type (divmod (b , f8 ), tuple [np .float64 , np .float64 ])
141
- assert_type (divmod (f , f8 ), tuple [np .float64 , np .float64 ])
142
176
assert_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 ])
144
177
assert_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