Skip to content

Commit 190e645

Browse files
authored
🐛✨ sparse: fix and improve the lil_matrix and lil_array constructors (#818)
2 parents 5ed42d4 + 3b3add0 commit 190e645

File tree

2 files changed

+200
-29
lines changed

2 files changed

+200
-29
lines changed

scipy-stubs/sparse/_lil.pyi

Lines changed: 139 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from collections.abc import Sequence
2-
from typing import Any, Generic, Literal, Self, TypeAlias, overload, type_check_only
2+
from typing import Any, Generic, Literal, Self, SupportsIndex, TypeAlias, overload, type_check_only
33
from typing_extensions import TypeIs, TypeVar, override
44

55
import numpy as np
66
import numpy.typing as npt
7-
import optype as op
87
import optype.numpy as onp
98
import optype.numpy.compat as npc
109

@@ -21,8 +20,9 @@ _T = TypeVar("_T")
2120
_ScalarT = TypeVar("_ScalarT", bound=npc.number | np.bool_)
2221
_ScalarT_co = TypeVar("_ScalarT_co", bound=npc.number | np.bool_, default=Any, covariant=True)
2322

24-
_ToMatrixPy: TypeAlias = Sequence[_T] | Sequence[Sequence[_T]]
23+
_ToMatrixPy: TypeAlias = list[_T] | list[list[_T]] # intentionally invariant
2524
_ToMatrix: TypeAlias = _spbase[_ScalarT] | onp.CanArrayND[_ScalarT] | Sequence[onp.CanArrayND[_ScalarT]] | _ToMatrixPy[_ScalarT]
25+
_ToAnyLIL: TypeAlias = _ToShape2D | _ToMatrix[npc.number | np.bool_]
2626

2727
###
2828

@@ -75,7 +75,7 @@ class _lil_base(_spbase[_ScalarT_co, tuple[int, int]], IndexMixin[_ScalarT_co, t
7575
@overload
7676
def count_nonzero(self, /, axis: None = None) -> np.intp: ...
7777
@overload
78-
def count_nonzero(self, /, axis: op.CanIndex) -> onp.Array1D[np.intp]: ...
78+
def count_nonzero(self, /, axis: SupportsIndex) -> onp.Array1D[np.intp]: ...
7979

8080
#
8181
def getrowview(self, /, i: int) -> Self: ...
@@ -98,7 +98,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
9898
/,
9999
arg1: _ToMatrix[_ScalarT_co],
100100
shape: _ToShape2D | None = None,
101-
dtype: None = None,
101+
dtype: onp.ToDType[_ScalarT_co] | None = None,
102102
copy: bool = False,
103103
*,
104104
maxprint: int | None = None,
@@ -118,7 +118,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
118118
def __init__(
119119
self: lil_array[np.bool_],
120120
/,
121-
arg1: _ToShape2D,
121+
arg1: _ToAnyLIL,
122122
shape: _ToShape2D | None,
123123
dtype: onp.AnyBoolDType,
124124
copy: bool = False,
@@ -129,7 +129,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
129129
def __init__(
130130
self: lil_array[np.bool_],
131131
/,
132-
arg1: _ToShape2D,
132+
arg1: _ToAnyLIL,
133133
shape: _ToShape2D | None = None,
134134
*,
135135
dtype: onp.AnyBoolDType,
@@ -138,9 +138,9 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
138138
) -> None: ...
139139
@overload # 2-d shape-like, dtype: int-like (positional)
140140
def __init__(
141-
self: lil_array[np.int64],
141+
self: lil_array[np.int_],
142142
/,
143-
arg1: _ToShape2D,
143+
arg1: _ToAnyLIL,
144144
shape: _ToShape2D | None,
145145
dtype: onp.AnyIntDType,
146146
copy: bool = False,
@@ -149,20 +149,42 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
149149
) -> None: ...
150150
@overload # 2-d shape-like, dtype: int-like (keyword)
151151
def __init__(
152-
self: lil_array[np.int64],
152+
self: lil_array[np.int_],
153153
/,
154-
arg1: _ToShape2D,
154+
arg1: _ToAnyLIL,
155155
shape: _ToShape2D | None = None,
156156
*,
157157
dtype: onp.AnyIntDType,
158158
copy: bool = False,
159159
maxprint: int | None = None,
160160
) -> None: ...
161+
@overload # 2-d shape-like, dtype: float64-like (positional)
162+
def __init__(
163+
self: lil_array[np.float64],
164+
/,
165+
arg1: _ToAnyLIL,
166+
shape: _ToShape2D | None,
167+
dtype: onp.AnyFloat64DType,
168+
copy: bool = False,
169+
*,
170+
maxprint: int | None = None,
171+
) -> None: ...
172+
@overload # 2-d shape-like, dtype: float64-like (keyword)
173+
def __init__(
174+
self: lil_array[np.float64],
175+
/,
176+
arg1: _ToAnyLIL,
177+
shape: _ToShape2D | None = None,
178+
*,
179+
dtype: onp.AnyFloat64DType,
180+
copy: bool = False,
181+
maxprint: int | None = None,
182+
) -> None: ...
161183
@overload # 2-d shape-like, dtype: complex128-like (positional)
162184
def __init__(
163185
self: lil_array[np.complex128],
164186
/,
165-
arg1: _ToShape2D,
187+
arg1: _ToAnyLIL,
166188
shape: _ToShape2D | None,
167189
dtype: onp.AnyComplex128DType,
168190
copy: bool = False,
@@ -173,7 +195,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
173195
def __init__(
174196
self: lil_array[np.complex128],
175197
/,
176-
arg1: _ToShape2D,
198+
arg1: _ToAnyLIL,
177199
shape: _ToShape2D | None = None,
178200
*,
179201
dtype: onp.AnyComplex128DType,
@@ -195,7 +217,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
195217
def __init__(
196218
self: lil_array[np.int_],
197219
/,
198-
arg1: _ToMatrixPy[op.JustInt],
220+
arg1: _ToMatrixPy[int],
199221
shape: _ToShape2D | None = None,
200222
dtype: onp.AnyIntDType | None = None,
201223
copy: bool = False,
@@ -206,7 +228,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
206228
def __init__(
207229
self: lil_array[np.float64],
208230
/,
209-
arg1: _ToMatrixPy[op.JustFloat],
231+
arg1: _ToMatrixPy[float],
210232
shape: _ToShape2D | None = None,
211233
dtype: onp.AnyFloat64DType | None = None,
212234
copy: bool = False,
@@ -217,7 +239,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
217239
def __init__(
218240
self: lil_array[np.complex128],
219241
/,
220-
arg1: _ToMatrixPy[op.JustComplex],
242+
arg1: _ToMatrixPy[complex],
221243
shape: _ToShape2D | None = None,
222244
dtype: onp.AnyComplex128DType | None = None,
223245
copy: bool = False,
@@ -228,7 +250,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
228250
def __init__(
229251
self,
230252
/,
231-
arg1: onp.ToComplex2D,
253+
arg1: _ToAnyLIL,
232254
shape: _ToShape2D | None,
233255
dtype: onp.ToDType[_ScalarT_co],
234256
copy: bool = False,
@@ -239,7 +261,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
239261
def __init__(
240262
self,
241263
/,
242-
arg1: onp.ToComplex2D,
264+
arg1: _ToAnyLIL,
243265
shape: _ToShape2D | None = None,
244266
*,
245267
dtype: onp.ToDType[_ScalarT_co],
@@ -250,7 +272,7 @@ class lil_array(_lil_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G
250272
def __init__(
251273
self,
252274
/,
253-
arg1: onp.ToComplex2D,
275+
arg1: _ToAnyLIL,
254276
shape: _ToShape2D | None = None,
255277
dtype: npt.DTypeLike | None = None,
256278
copy: bool = False,
@@ -279,22 +301,110 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
279301
/,
280302
arg1: _ToMatrix[_ScalarT_co],
281303
shape: _ToShape2D | None = None,
282-
dtype: None = None,
304+
dtype: onp.ToDType[_ScalarT_co] | None = None,
283305
copy: bool = False,
284306
*,
285307
maxprint: int | None = None,
286308
) -> None: ...
287-
@overload # 2-d shape-like, dtype: None
309+
@overload # 2-d shape-like, dtype: float64-like | None
288310
def __init__(
289311
self: lil_matrix[np.float64],
290312
/,
291313
arg1: _ToShape2D,
292-
shape: None = None,
314+
shape: _ToShape2D | None = None,
293315
dtype: onp.AnyFloat64DType | None = None,
294316
copy: bool = False,
295317
*,
296318
maxprint: int | None = None,
297319
) -> None: ...
320+
@overload # 2-d shape-like, dtype: bool-like (positional)
321+
def __init__(
322+
self: lil_matrix[np.bool_],
323+
/,
324+
arg1: _ToAnyLIL,
325+
shape: _ToShape2D | None,
326+
dtype: onp.AnyBoolDType,
327+
copy: bool = False,
328+
*,
329+
maxprint: int | None = None,
330+
) -> None: ...
331+
@overload # 2-d shape-like, dtype: bool-like (keyword)
332+
def __init__(
333+
self: lil_matrix[np.bool_],
334+
/,
335+
arg1: _ToAnyLIL,
336+
shape: _ToShape2D | None = None,
337+
*,
338+
dtype: onp.AnyBoolDType,
339+
copy: bool = False,
340+
maxprint: int | None = None,
341+
) -> None: ...
342+
@overload # 2-d shape-like, dtype: int-like (positional)
343+
def __init__(
344+
self: lil_matrix[np.int_],
345+
/,
346+
arg1: _ToAnyLIL,
347+
shape: _ToShape2D | None,
348+
dtype: onp.AnyIntDType,
349+
copy: bool = False,
350+
*,
351+
maxprint: int | None = None,
352+
) -> None: ...
353+
@overload # 2-d shape-like, dtype: int-like (keyword)
354+
def __init__(
355+
self: lil_matrix[np.int_],
356+
/,
357+
arg1: _ToAnyLIL,
358+
shape: _ToShape2D | None = None,
359+
*,
360+
dtype: onp.AnyIntDType,
361+
copy: bool = False,
362+
maxprint: int | None = None,
363+
) -> None: ...
364+
@overload # 2-d shape-like, dtype: float64-like (positional)
365+
def __init__(
366+
self: lil_matrix[np.float64],
367+
/,
368+
arg1: _ToAnyLIL,
369+
shape: _ToShape2D | None,
370+
dtype: onp.AnyFloat64DType,
371+
copy: bool = False,
372+
*,
373+
maxprint: int | None = None,
374+
) -> None: ...
375+
@overload # 2-d shape-like, dtype: float64-like (keyword)
376+
def __init__(
377+
self: lil_matrix[np.float64],
378+
/,
379+
arg1: _ToAnyLIL,
380+
shape: _ToShape2D | None = None,
381+
*,
382+
dtype: onp.AnyFloat64DType,
383+
copy: bool = False,
384+
maxprint: int | None = None,
385+
) -> None: ...
386+
@overload # 2-d shape-like, dtype: complex128-like (positional)
387+
def __init__(
388+
self: lil_matrix[np.complex128],
389+
/,
390+
arg1: _ToAnyLIL,
391+
shape: _ToShape2D | None,
392+
dtype: onp.AnyComplex128DType,
393+
copy: bool = False,
394+
*,
395+
maxprint: int | None = None,
396+
) -> None: ...
397+
@overload # 2-d shape-like, dtype: complex128-like (keyword)
398+
def __init__(
399+
self: lil_matrix[np.complex128],
400+
/,
401+
arg1: _ToAnyLIL,
402+
shape: _ToShape2D | None = None,
403+
*,
404+
dtype: onp.AnyComplex128DType,
405+
copy: bool = False,
406+
maxprint: int | None = None,
407+
) -> None: ...
298408
@overload # matrix-like builtins.bool, dtype: bool-like | None
299409
def __init__(
300410
self: lil_matrix[np.bool_],
@@ -310,7 +420,7 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
310420
def __init__(
311421
self: lil_matrix[np.int_],
312422
/,
313-
arg1: _ToMatrixPy[op.JustInt],
423+
arg1: _ToMatrixPy[int],
314424
shape: _ToShape2D | None = None,
315425
dtype: onp.AnyIntDType | None = None,
316426
copy: bool = False,
@@ -321,7 +431,7 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
321431
def __init__(
322432
self: lil_matrix[np.float64],
323433
/,
324-
arg1: _ToMatrixPy[op.JustFloat],
434+
arg1: _ToMatrixPy[float],
325435
shape: _ToShape2D | None = None,
326436
dtype: onp.AnyFloat64DType | None = None,
327437
copy: bool = False,
@@ -332,7 +442,7 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
332442
def __init__(
333443
self: lil_matrix[np.complex128],
334444
/,
335-
arg1: _ToMatrixPy[op.JustComplex],
445+
arg1: _ToMatrixPy[complex],
336446
shape: _ToShape2D | None = None,
337447
dtype: onp.AnyComplex128DType | None = None,
338448
copy: bool = False,
@@ -343,7 +453,7 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
343453
def __init__(
344454
self,
345455
/,
346-
arg1: onp.ToComplex2D,
456+
arg1: _ToAnyLIL,
347457
shape: _ToShape2D | None,
348458
dtype: onp.ToDType[_ScalarT_co],
349459
copy: bool = False,
@@ -354,7 +464,7 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
354464
def __init__(
355465
self,
356466
/,
357-
arg1: onp.ToComplex2D,
467+
arg1: _ToAnyLIL,
358468
shape: _ToShape2D | None = None,
359469
*,
360470
dtype: onp.ToDType[_ScalarT_co],
@@ -365,7 +475,7 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
365475
def __init__(
366476
self,
367477
/,
368-
arg1: onp.ToComplex2D,
478+
arg1: _ToAnyLIL,
369479
shape: _ToShape2D | None = None,
370480
dtype: npt.DTypeLike | None = None,
371481
copy: bool = False,
@@ -381,7 +491,7 @@ class lil_matrix(_lil_base[_ScalarT_co], spmatrix[_ScalarT_co], Generic[_ScalarT
381491
@overload
382492
def getnnz(self, /, axis: None = None) -> int: ...
383493
@overload
384-
def getnnz(self, /, axis: op.CanIndex) -> onp.Array1D[np.int32]: ...
494+
def getnnz(self, /, axis: SupportsIndex) -> onp.Array1D[np.int32]: ...
385495

386496
#
387497
def isspmatrix_lil(x: object) -> TypeIs[lil_matrix]: ...

0 commit comments

Comments
 (0)