Skip to content

Commit 51028b3

Browse files
committed
🐛 sparse: add __len__ to _spbase (kinda)
1 parent 830cc29 commit 51028b3

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

scipy-stubs/sparse/_base.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ class _spbase(Generic[_SCT_co, _ShapeT_co]):
179179
#
180180
def __bool__(self, /) -> bool: ...
181181

182+
# only `dok_*` doesn't raise `TypeError` when passed to `len`
183+
def __len__(self: dok_array[Any, Any] | dok_matrix[Any], /) -> int: ... # pyright: ignore[reportGeneralTypeIssues]
184+
182185
#
183186
@overload # 1-d {csr,dok}_array
184187
def __iter__(self: csr_array[_SCT, _1D] | dok_array[_SCT, _1D], /) -> Iterator[_SCT]: ...

scipy-stubs/sparse/_dok.pyi

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
143143
@override
144144
def todok(self, /, copy: bool = False) -> Self: ...
145145

146+
#
147+
@override
148+
def __len__(self, /) -> int: ...
149+
146150
#
147151
@overload
148152
def __delitem__(self: _dok_base[Any, _2D], key: _ToKey2D, /) -> None: ...
@@ -185,59 +189,69 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
185189

186190
#
187191
@overload
188-
def get(self: _dok_base[Any, _2D], key: _ToKey2D, /, default: _T) -> _SCT | _T: ...
192+
def get(self: _dok_base[Any, _2D], /, key: _ToKey2D, default: _T) -> _SCT | _T: ...
189193
@overload
190-
def get(self: _dok_base[Any, _2D], key: _ToKey2D, /, default: float = 0.0) -> _SCT | float: ...
194+
def get(self: _dok_base[Any, _2D], /, key: _ToKey2D, default: float = 0.0) -> _SCT | float: ...
191195
@overload
192-
def get(self: _dok_base[Any, _1D], key: _ToKey1D, /, default: _T) -> _SCT | _T: ...
196+
def get(self: _dok_base[Any, _1D], /, key: _ToKey1D, default: _T) -> _SCT | _T: ...
193197
@overload
194-
def get(self: _dok_base[Any, _1D], key: _ToKey1D, /, default: float = 0.0) -> _SCT | float: ...
198+
def get(self: _dok_base[Any, _1D], /, key: _ToKey1D, default: float = 0.0) -> _SCT | float: ...
195199
@overload
196-
def get(self, key: _ToKey1D | _ToKey2D, /, default: _T) -> _SCT | _T: ...
200+
def get(self, /, key: _ToKey1D | _ToKey2D, default: _T) -> _SCT | _T: ...
197201
@overload
198-
def get(self, key: _ToKey1D | _ToKey2D, /, default: float = 0.0) -> _SCT | float: ...
202+
def get(self, /, key: _ToKey1D | _ToKey2D, default: float = 0.0) -> _SCT | float: ...
199203

200204
#
201205
def conjtransp(self, /) -> Self: ...
202206

203207
#
204208
@overload
205209
@classmethod
206-
def fromkeys(cls: type[_dok_base[_SCT, _2D]], ks: _ToKeys2D, v: _SCT, /) -> _dok_base[_SCT, _2D]: ...
210+
def fromkeys(cls: type[_dok_base[_SCT, _2D]], iterable: _ToKeys2D, v: _SCT, /) -> _dok_base[_SCT, _2D]: ...
207211
@overload
208212
@classmethod
209-
def fromkeys(cls: type[_dok_base[_SCT, _1D]], ks: _ToKeys1D, v: _SCT, /) -> _dok_base[_SCT, _1D]: ...
213+
def fromkeys(cls: type[_dok_base[_SCT, _1D]], iterable: _ToKeys1D, v: _SCT, /) -> _dok_base[_SCT, _1D]: ...
210214
@overload
211215
@classmethod
212-
def fromkeys(cls: type[_dok_base[np.bool_, _2D]], ks: _ToKeys2D, v: onp.ToBool, /) -> _dok_base[np.bool_, _2D]: ...
216+
def fromkeys(cls: type[_dok_base[np.bool_, _2D]], iterable: _ToKeys2D, v: onp.ToBool, /) -> _dok_base[np.bool_, _2D]: ...
213217
@overload
214218
@classmethod
215-
def fromkeys(cls: type[_dok_base[np.bool_, _1D]], ks: _ToKeys1D, v: onp.ToBool, /) -> _dok_base[np.bool_, _1D]: ...
219+
def fromkeys(cls: type[_dok_base[np.bool_, _1D]], iterable: _ToKeys1D, v: onp.ToBool, /) -> _dok_base[np.bool_, _1D]: ...
216220
@overload
217221
@classmethod
218-
def fromkeys(cls: type[_dok_base[np.int_, _2D]], ks: _ToKeys2D, v: opt.JustInt = 1, /) -> _dok_base[np.int_, _2D]: ...
222+
def fromkeys(cls: type[_dok_base[np.int_, _2D]], iterable: _ToKeys2D, v: opt.JustInt = 1, /) -> _dok_base[np.int_, _2D]: ...
219223
@overload
220224
@classmethod
221-
def fromkeys(cls: type[_dok_base[np.int_, _1D]], ks: _ToKeys1D, v: opt.JustInt = 1, /) -> _dok_base[np.int_, _1D]: ...
225+
def fromkeys(cls: type[_dok_base[np.int_, _1D]], iterable: _ToKeys1D, v: opt.JustInt = 1, /) -> _dok_base[np.int_, _1D]: ...
222226
@overload
223227
@classmethod
224-
def fromkeys(cls: type[_dok_base[np.float64, _2D]], ks: _ToKeys2D, v: opt.JustFloat, /) -> _dok_base[np.float64, _2D]: ...
228+
def fromkeys(
229+
cls: type[_dok_base[np.float64, _2D]],
230+
iterable: _ToKeys2D,
231+
v: opt.JustFloat,
232+
/,
233+
) -> _dok_base[np.float64, _2D]: ...
225234
@overload
226235
@classmethod
227-
def fromkeys(cls: type[_dok_base[np.float64, _1D]], ks: _ToKeys1D, v: opt.JustFloat, /) -> _dok_base[np.float64, _1D]: ...
236+
def fromkeys(
237+
cls: type[_dok_base[np.float64, _1D]],
238+
iterable: _ToKeys1D,
239+
v: opt.JustFloat,
240+
/,
241+
) -> _dok_base[np.float64, _1D]: ...
228242
@overload
229243
@classmethod
230244
def fromkeys(
231245
cls: type[_dok_base[np.complex128, _2D]],
232-
ks: _ToKeys2D,
246+
iterable: _ToKeys2D,
233247
v: opt.JustComplex,
234248
/,
235249
) -> _dok_base[np.complex128, _2D]: ...
236250
@overload
237251
@classmethod
238252
def fromkeys(
239253
cls: type[_dok_base[np.complex128, _1D]],
240-
ks: _ToKeys1D,
254+
iterable: _ToKeys1D,
241255
v: opt.JustComplex,
242256
/,
243257
) -> _dok_base[np.complex128, _1D]: ...
@@ -248,68 +262,83 @@ class dok_array(_dok_base[_SCT, _ShapeT_co], sparray, Generic[_SCT, _ShapeT_co])
248262
# https://github.com/python/typing/issues/548
249263
@overload
250264
@classmethod
251-
def fromkeys(cls: type[dok_array[_SCT, _2D]], ks: _ToKeys2D, v: _SCT, /) -> dok_array[_SCT, _2D]: ...
265+
def fromkeys(cls: type[dok_array[_SCT, _2D]], iterable: _ToKeys2D, v: _SCT, /) -> dok_array[_SCT, _2D]: ...
252266
@overload
253267
@classmethod
254-
def fromkeys(cls: type[dok_array[_SCT, _1D]], ks: _ToKeys1D, v: _SCT, /) -> dok_array[_SCT, _1D]: ...
268+
def fromkeys(cls: type[dok_array[_SCT, _1D]], iterable: _ToKeys1D, v: _SCT, /) -> dok_array[_SCT, _1D]: ...
255269
@overload
256270
@classmethod
257-
def fromkeys(cls: type[dok_array[np.bool_, _2D]], ks: _ToKeys2D, v: onp.ToBool, /) -> dok_array[np.bool_, _2D]: ...
271+
def fromkeys(cls: type[dok_array[np.bool_, _2D]], iterable: _ToKeys2D, v: onp.ToBool, /) -> dok_array[np.bool_, _2D]: ...
258272
@overload
259273
@classmethod
260-
def fromkeys(cls: type[dok_array[np.bool_, _1D]], ks: _ToKeys1D, v: onp.ToBool, /) -> dok_array[np.bool_, _1D]: ...
274+
def fromkeys(cls: type[dok_array[np.bool_, _1D]], iterable: _ToKeys1D, v: onp.ToBool, /) -> dok_array[np.bool_, _1D]: ...
261275
@overload
262276
@classmethod
263-
def fromkeys(cls: type[dok_array[np.int_, _2D]], ks: _ToKeys2D, v: opt.JustInt = 1, /) -> dok_array[np.int_, _2D]: ...
277+
def fromkeys(cls: type[dok_array[np.int_, _2D]], iterable: _ToKeys2D, v: opt.JustInt = 1, /) -> dok_array[np.int_, _2D]: ...
264278
@overload
265279
@classmethod
266-
def fromkeys(cls: type[dok_array[np.int_, _1D]], ks: _ToKeys1D, v: opt.JustInt = 1, /) -> dok_array[np.int_, _1D]: ...
280+
def fromkeys(cls: type[dok_array[np.int_, _1D]], iterable: _ToKeys1D, v: opt.JustInt = 1, /) -> dok_array[np.int_, _1D]: ...
267281
@overload
268282
@classmethod
269-
def fromkeys(cls: type[dok_array[np.float64, _2D]], ks: _ToKeys2D, v: opt.JustFloat, /) -> dok_array[np.float64, _2D]: ...
283+
def fromkeys(
284+
cls: type[dok_array[np.float64, _2D]],
285+
iterable: _ToKeys2D,
286+
v: opt.JustFloat,
287+
/,
288+
) -> dok_array[np.float64, _2D]: ...
270289
@overload
271290
@classmethod
272-
def fromkeys(cls: type[dok_array[np.float64, _1D]], ks: _ToKeys1D, v: opt.JustFloat, /) -> dok_array[np.float64, _1D]: ...
291+
def fromkeys(
292+
cls: type[dok_array[np.float64, _1D]],
293+
iterable: _ToKeys1D,
294+
v: opt.JustFloat,
295+
/,
296+
) -> dok_array[np.float64, _1D]: ...
273297
@overload
274298
@classmethod
275299
def fromkeys(
276300
cls: type[dok_array[np.complex128, _2D]],
277-
ks: _ToKeys2D,
301+
iterable: _ToKeys2D,
278302
v: opt.JustComplex,
279303
/,
280304
) -> dok_array[np.complex128, _2D]: ...
281305
@overload
282306
@classmethod
283307
def fromkeys(
284308
cls: type[dok_array[np.complex128, _1D]],
285-
ks: _ToKeys1D,
309+
iterable: _ToKeys1D,
286310
v: opt.JustComplex,
287311
/,
288312
) -> dok_array[np.complex128, _1D]: ...
289313

290314
#
291315
class dok_matrix(_dok_base[_SCT, _2D], spmatrix[_SCT], Generic[_SCT]):
292316
@override
293-
def get(self, key: _ToKey2D, /, default: onp.ToComplex = 0.0) -> _SCT: ...
317+
def get(self, /, key: _ToKey2D, default: onp.ToComplex = 0.0) -> _SCT: ...
294318
@override
295319
def setdefault(self, key: _ToKey2D, default: onp.ToComplex | None = None, /) -> _SCT: ...
296320

297321
#
298322
@overload
299323
@classmethod
300-
def fromkeys(cls, ks: _ToKeys2D, v: _SCT, /) -> Self: ...
324+
def fromkeys(cls, iterable: _ToKeys2D, v: _SCT, /) -> Self: ...
301325
@overload
302326
@classmethod
303-
def fromkeys(cls: type[dok_matrix[np.bool_]], ks: _ToKeys2D, v: onp.ToBool, /) -> dok_matrix[np.bool_]: ...
327+
def fromkeys(cls: type[dok_matrix[np.bool_]], iterable: _ToKeys2D, v: onp.ToBool, /) -> dok_matrix[np.bool_]: ...
304328
@overload
305329
@classmethod
306-
def fromkeys(cls: type[dok_matrix[np.int_]], ks: _ToKeys2D, v: opt.JustInt = 1, /) -> dok_matrix[np.int_]: ...
330+
def fromkeys(cls: type[dok_matrix[np.int_]], iterable: _ToKeys2D, v: opt.JustInt = 1, /) -> dok_matrix[np.int_]: ...
307331
@overload
308332
@classmethod
309-
def fromkeys(cls: type[dok_matrix[np.float64]], ks: _ToKeys2D, v: opt.JustFloat, /) -> dok_matrix[np.float64]: ...
333+
def fromkeys(cls: type[dok_matrix[np.float64]], iterable: _ToKeys2D, v: opt.JustFloat, /) -> dok_matrix[np.float64]: ...
310334
@overload
311335
@classmethod
312-
def fromkeys(cls: type[dok_matrix[np.complex128]], ks: _ToKeys2D, v: opt.JustComplex, /) -> dok_matrix[np.complex128]: ...
336+
def fromkeys(
337+
cls: type[dok_matrix[np.complex128]],
338+
iterable: _ToKeys2D,
339+
v: opt.JustComplex,
340+
/,
341+
) -> dok_matrix[np.complex128]: ...
313342

314343
#
315344
def isspmatrix_dok(x: object) -> TypeIs[dok_matrix]: ...

0 commit comments

Comments
 (0)