1
- # mypy: disable-error-code="misc, override"
1
+ # NOTE: Adding `@override` to `@overload`ed methods will crash stubtest (basedmypy 1.13.0)
2
+ # mypy: disable-error-code="misc, override, explicit-override"
2
3
# pyright: reportIncompatibleMethodOverride=false
3
4
4
5
from collections .abc import Iterable , Sequence
5
6
from typing import Any , Generic , Literal , TypeAlias , overload
6
7
from typing_extensions import Never , Self , TypeIs , TypeVar , override
7
8
8
9
import numpy as np
10
+ import optype as op
9
11
import optype .numpy as onp
10
12
import optype .typing as opt
11
13
from ._base import _spbase , sparray
@@ -15,14 +17,25 @@ from ._typing import Scalar, ShapeDOK, ToShape1dNd
15
17
16
18
__all__ = ["dok_array" , "dok_matrix" , "isspmatrix_dok" ]
17
19
20
+ ###
21
+
18
22
_T = TypeVar ("_T" )
19
23
_SCT = TypeVar ("_SCT" , bound = Scalar , default = Any )
20
24
_ShapeT_co = TypeVar ("_ShapeT_co" , bound = ShapeDOK , default = ShapeDOK , covariant = True )
21
25
26
+ _1D : TypeAlias = tuple [int ] # noqa: PYI042
27
+ _2D : TypeAlias = tuple [int , int ] # noqa: PYI042
28
+
22
29
_ToDType : TypeAlias = type [_SCT ] | np .dtype [_SCT ] | onp .HasDType [np .dtype [_SCT ]]
23
30
_ToMatrix : TypeAlias = _spbase [_SCT ] | onp .CanArrayND [_SCT ] | Sequence [onp .CanArrayND [_SCT ]] | _ToMatrixPy [_SCT ]
24
31
_ToMatrixPy : TypeAlias = Sequence [_T ] | Sequence [Sequence [_T ]]
25
32
33
+ _ToKey1D : TypeAlias = onp .ToJustInt | tuple [onp .ToJustInt ]
34
+ _ToKey2D : TypeAlias = tuple [onp .ToJustInt , onp .ToJustInt ]
35
+
36
+ _ToKeys1D : TypeAlias = Iterable [_ToKey1D ]
37
+ _ToKeys2D : TypeAlias = Iterable [_ToKey2D ]
38
+
26
39
###
27
40
28
41
class _dok_base (_spbase [_SCT , _ShapeT_co ], IndexMixin [_SCT , _ShapeT_co ], dict [ShapeDOK , _SCT ], Generic [_SCT , _ShapeT_co ]):
@@ -31,11 +44,9 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
31
44
@property
32
45
@override
33
46
def format (self , / ) -> Literal ["dok" ]: ...
34
- #
35
47
@property
36
48
@override
37
49
def ndim (self , / ) -> Literal [1 , 2 ]: ...
38
- #
39
50
@property
40
51
@override
41
52
def shape (self , / ) -> _ShapeT_co : ...
@@ -141,27 +152,158 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
141
152
def __ror__ (self , other : Never , / ) -> Never : ...
142
153
@override
143
154
def __ior__ (self , other : Never , / ) -> Never : ... # noqa: PYI034
155
+
156
+ #
157
+ @overload
158
+ def count_nonzero (self , / , axis : None = None ) -> int : ...
159
+ @overload
160
+ def count_nonzero (self , / , axis : op .CanIndex ) -> onp .Array1D [np .intp ]: ...
161
+
162
+ #
144
163
@override
145
164
def update (self , / , val : Never ) -> Never : ...
146
165
147
- # TODO(jorenham)
148
- @override
149
- def get (self , key : onp .ToJustInt | ShapeDOK , / , default : onp .ToComplex = 0.0 ) -> _SCT : ...
150
- @override
151
- def setdefault (self , key : onp .ToJustInt | ShapeDOK , default : onp .ToComplex | None = None , / ) -> _SCT : ...
152
- @classmethod
153
- @override
154
- def fromkeys (cls , iterable : Iterable [ShapeDOK ], value : int = 1 , / ) -> Self : ...
166
+ #
167
+ @overload
168
+ def setdefault (self : _dok_base [Any , _1D ], key : _ToKey1D , default : None = None , / ) -> _SCT | None : ...
169
+ @overload
170
+ def setdefault (self : _dok_base [Any , _2D ], key : _ToKey2D , default : None = None , / ) -> _SCT | None : ...
171
+ @overload
172
+ def setdefault (self , key : _ToKey1D | _ToKey2D , default : None = None , / ) -> _SCT | None : ...
173
+ @overload
174
+ def setdefault (self : _dok_base [Any , _1D ], key : _ToKey1D , default : _T , / ) -> _SCT | _T : ...
175
+ @overload
176
+ def setdefault (self : _dok_base [Any , _2D ], key : _ToKey2D , default : _T , / ) -> _SCT | _T : ...
177
+ @overload
178
+ def setdefault (self , key : _ToKey1D | _ToKey2D , default : _T , / ) -> _SCT | _T : ...
179
+
180
+ #
181
+ @overload
182
+ def get (self : _dok_base [Any , _1D ], key : _ToKey1D , / , default : float = 0.0 ) -> _SCT | float : ...
183
+ @overload
184
+ def get (self : _dok_base [Any , _2D ], key : _ToKey2D , / , default : float = 0.0 ) -> _SCT | float : ...
185
+ @overload
186
+ def get (self , key : _ToKey1D | _ToKey2D , / , default : float = 0.0 ) -> _SCT | float : ...
187
+ @overload
188
+ def get (self : _dok_base [Any , _1D ], key : _ToKey1D , / , default : _T ) -> _SCT | _T : ...
189
+ @overload
190
+ def get (self : _dok_base [Any , _2D ], key : _ToKey2D , / , default : _T ) -> _SCT | _T : ...
191
+ @overload
192
+ def get (self , key : _ToKey1D | _ToKey2D , / , default : _T ) -> _SCT | _T : ...
155
193
156
194
#
157
195
def conjtransp (self , / ) -> Self : ...
158
196
159
- class dok_array (_dok_base [_SCT , _ShapeT_co ], sparray , Generic [_SCT , _ShapeT_co ]): ...
197
+ #
198
+ @overload
199
+ @classmethod
200
+ def fromkeys (cls : type [_dok_base [np .bool_ , _1D ]], ks : _ToKeys1D , v : onp .ToBool , / ) -> _dok_base [np .bool_ , _1D ]: ...
201
+ @overload
202
+ @classmethod
203
+ def fromkeys (cls : type [_dok_base [np .bool_ , _2D ]], ks : _ToKeys2D , v : onp .ToBool , / ) -> _dok_base [np .bool_ , _2D ]: ...
204
+ @overload
205
+ @classmethod
206
+ def fromkeys (cls : type [_dok_base [np .int_ , _1D ]], ks : _ToKeys1D , v : opt .JustInt = 1 , / ) -> _dok_base [np .int_ , _1D ]: ...
207
+ @overload
208
+ @classmethod
209
+ def fromkeys (cls : type [_dok_base [np .int_ , _2D ]], ks : _ToKeys2D , v : opt .JustInt = 1 , / ) -> _dok_base [np .int_ , _2D ]: ...
210
+ @overload
211
+ @classmethod
212
+ def fromkeys (cls : type [_dok_base [np .float64 , _1D ]], ks : _ToKeys1D , v : opt .JustFloat , / ) -> _dok_base [np .float64 , _1D ]: ...
213
+ @overload
214
+ @classmethod
215
+ def fromkeys (cls : type [_dok_base [np .float64 , _2D ]], ks : _ToKeys2D , v : opt .JustFloat , / ) -> _dok_base [np .float64 , _2D ]: ...
216
+ @overload
217
+ @classmethod
218
+ def fromkeys (
219
+ cls : type [_dok_base [np .complex128 , _1D ]],
220
+ ks : _ToKeys1D ,
221
+ v : opt .JustComplex ,
222
+ / ,
223
+ ) -> _dok_base [np .complex128 , _1D ]: ...
224
+ @overload
225
+ @classmethod
226
+ def fromkeys (
227
+ cls : type [_dok_base [np .complex128 , _2D ]],
228
+ ks : _ToKeys2D ,
229
+ v : opt .JustComplex ,
230
+ / ,
231
+ ) -> _dok_base [np .complex128 , _2D ]: ...
232
+ @overload
233
+ @classmethod
234
+ def fromkeys (cls : type [_dok_base [_SCT , _1D ]], ks : _ToKeys1D , v : _SCT , / ) -> _dok_base [_SCT , _1D ]: ...
235
+ @overload
236
+ @classmethod
237
+ def fromkeys (cls : type [_dok_base [_SCT , _2D ]], ks : _ToKeys2D , v : _SCT , / ) -> _dok_base [_SCT , _2D ]: ...
160
238
161
- class dok_matrix (_dok_base [_SCT , tuple [int , int ]], spmatrix [_SCT ], Generic [_SCT ]):
239
+ #
240
+ class dok_array (_dok_base [_SCT , _ShapeT_co ], sparray , Generic [_SCT , _ShapeT_co ]):
241
+ # NOTE: This horrible code duplication is required due to the lack of higher-kinded typing (HKT) support.
242
+ # https://github.com/python/typing/issues/548
243
+ @overload
244
+ @classmethod
245
+ def fromkeys (cls : type [dok_array [np .bool_ , _1D ]], ks : _ToKeys1D , v : onp .ToBool , / ) -> dok_array [np .bool_ , _1D ]: ...
246
+ @overload
247
+ @classmethod
248
+ def fromkeys (cls : type [dok_array [np .bool_ , _2D ]], ks : _ToKeys2D , v : onp .ToBool , / ) -> dok_array [np .bool_ , _2D ]: ...
249
+ @overload
250
+ @classmethod
251
+ def fromkeys (cls : type [dok_array [np .int_ , _1D ]], ks : _ToKeys1D , v : opt .JustInt = 1 , / ) -> dok_array [np .int_ , _1D ]: ...
252
+ @overload
253
+ @classmethod
254
+ def fromkeys (cls : type [dok_array [np .int_ , _2D ]], ks : _ToKeys2D , v : opt .JustInt = 1 , / ) -> dok_array [np .int_ , _2D ]: ...
255
+ @overload
256
+ @classmethod
257
+ def fromkeys (cls : type [dok_array [np .float64 , _1D ]], ks : _ToKeys1D , v : opt .JustFloat , / ) -> dok_array [np .float64 , _1D ]: ...
258
+ @overload
259
+ @classmethod
260
+ def fromkeys (cls : type [dok_array [np .float64 , _2D ]], ks : _ToKeys2D , v : opt .JustFloat , / ) -> dok_array [np .float64 , _2D ]: ...
261
+ @overload
262
+ @classmethod
263
+ def fromkeys (
264
+ cls : type [dok_array [np .complex128 , _1D ]],
265
+ ks : _ToKeys1D ,
266
+ v : opt .JustComplex ,
267
+ / ,
268
+ ) -> dok_array [np .complex128 , _1D ]: ...
269
+ @overload
270
+ @classmethod
271
+ def fromkeys (
272
+ cls : type [dok_array [np .complex128 , _2D ]],
273
+ ks : _ToKeys2D ,
274
+ v : opt .JustComplex ,
275
+ / ,
276
+ ) -> dok_array [np .complex128 , _2D ]: ...
277
+ @overload
278
+ @classmethod
279
+ def fromkeys (cls : type [dok_array [_SCT , _1D ]], ks : _ToKeys1D , v : _SCT , / ) -> dok_array [_SCT , _1D ]: ...
280
+ @overload
281
+ @classmethod
282
+ def fromkeys (cls : type [dok_array [_SCT , _2D ]], ks : _ToKeys2D , v : _SCT , / ) -> dok_array [_SCT , _2D ]: ...
283
+
284
+ #
285
+ class dok_matrix (_dok_base [_SCT , _2D ], spmatrix [_SCT ], Generic [_SCT ]):
162
286
@override
163
- def get (self , key : tuple [ onp . ToJustInt , onp . ToJustInt ] , / , default : onp .ToComplex = 0.0 ) -> _SCT : ...
287
+ def get (self , key : _ToKey2D , / , default : onp .ToComplex = 0.0 ) -> _SCT : ...
164
288
@override
165
- def setdefault (self , key : tuple [onp .ToJustInt , onp .ToJustInt ], default : onp .ToComplex | None = None , / ) -> _SCT : ...
289
+ def setdefault (self , key : _ToKey2D , default : onp .ToComplex | None = None , / ) -> _SCT : ...
290
+
291
+ #
292
+ @overload
293
+ @classmethod
294
+ def fromkeys (cls : type [dok_matrix [np .bool_ ]], ks : _ToKeys2D , v : onp .ToBool , / ) -> dok_matrix [np .bool_ ]: ...
295
+ @overload
296
+ @classmethod
297
+ def fromkeys (cls : type [dok_matrix [np .int_ ]], ks : _ToKeys2D , v : opt .JustInt = 1 , / ) -> dok_matrix [np .int_ ]: ...
298
+ @overload
299
+ @classmethod
300
+ def fromkeys (cls : type [dok_matrix [np .float64 ]], ks : _ToKeys2D , v : opt .JustFloat , / ) -> dok_matrix [np .float64 ]: ...
301
+ @overload
302
+ @classmethod
303
+ def fromkeys (cls : type [dok_matrix [np .complex128 ]], ks : _ToKeys2D , v : opt .JustComplex , / ) -> dok_matrix [np .complex128 ]: ...
304
+ @overload
305
+ @classmethod
306
+ def fromkeys (cls , ks : _ToKeys2D , v : _SCT , / ) -> Self : ...
166
307
308
+ #
167
309
def isspmatrix_dok (x : object ) -> TypeIs [dok_matrix ]: ...
0 commit comments