Skip to content

Commit 4c116f1

Browse files
authored
🎨 sparse: prefer optype.numpy over scipy._typing (#434)
2 parents ab0e391 + 2f300ab commit 4c116f1

34 files changed

+698
-672
lines changed

‎scipy-stubs/sparse/_base.pyi

Lines changed: 178 additions & 130 deletions
Large diffs are not rendered by default.

‎scipy-stubs/sparse/_bsr.pyi

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ from ._base import _spbase, sparray
99
from ._compressed import _cs_matrix
1010
from ._data import _minmax_mixin
1111
from ._matrix import spmatrix
12-
from ._typing import Int, Scalar, ToShape2d
12+
from ._typing import Integer, Numeric, ToShape2D
1313

1414
__all__ = ["bsr_array", "bsr_matrix", "isspmatrix_bsr"]
1515

1616
_T = TypeVar("_T")
17-
_SCT = TypeVar("_SCT", bound=Scalar, default=Any)
17+
_SCT = TypeVar("_SCT", bound=Numeric, default=Any)
1818

1919
_ToMatrix: TypeAlias = _spbase[_SCT] | onp.CanArrayND[_SCT] | Sequence[onp.CanArrayND[_SCT]] | _ToMatrixPy[_SCT]
2020
_ToMatrixPy: TypeAlias = Sequence[_T] | Sequence[Sequence[_T]]
2121

22-
_ToData2: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Int]]
23-
_ToData3: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Int], onp.ArrayND[Int]]
22+
_ToData2: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Integer]]
23+
_ToData3: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Integer], onp.ArrayND[Integer]]
2424
_ToData: TypeAlias = _ToData2[_SCT] | _ToData3[_SCT]
2525

2626
###
@@ -46,7 +46,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
4646
self,
4747
/,
4848
arg1: _ToMatrix[_SCT] | _ToData[_SCT],
49-
shape: ToShape2d | None = None,
49+
shape: ToShape2D | None = None,
5050
dtype: None = None,
5151
copy: bool = False,
5252
blocksize: tuple[int, int] | None = None,
@@ -57,7 +57,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
5757
def __init__(
5858
self: _bsr_base[np.float64],
5959
/,
60-
arg1: ToShape2d,
60+
arg1: ToShape2D,
6161
shape: None = None,
6262
dtype: None = None,
6363
copy: bool = False,
@@ -68,7 +68,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
6868
self: _bsr_base[np.bool_],
6969
/,
7070
arg1: _ToMatrixPy[bool],
71-
shape: ToShape2d | None = None,
71+
shape: ToShape2D | None = None,
7272
dtype: onp.AnyBoolDType | None = None,
7373
copy: bool = False,
7474
blocksize: tuple[int, int] | None = None,
@@ -80,7 +80,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
8080
self: _bsr_base[np.int_],
8181
/,
8282
arg1: _ToMatrixPy[op.JustInt],
83-
shape: ToShape2d | None = None,
83+
shape: ToShape2D | None = None,
8484
dtype: onp.AnyIntDType | None = None,
8585
copy: bool = False,
8686
blocksize: tuple[int, int] | None = None,
@@ -92,7 +92,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
9292
self: _bsr_base[np.float64],
9393
/,
9494
arg1: _ToMatrixPy[op.JustFloat],
95-
shape: ToShape2d | None = None,
95+
shape: ToShape2D | None = None,
9696
dtype: onp.AnyFloat64DType | None = None,
9797
copy: bool = False,
9898
blocksize: tuple[int, int] | None = None,
@@ -104,7 +104,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
104104
self: _bsr_base[np.complex128],
105105
/,
106106
arg1: _ToMatrixPy[op.JustComplex],
107-
shape: ToShape2d | None = None,
107+
shape: ToShape2D | None = None,
108108
dtype: onp.AnyComplex128DType | None = None,
109109
copy: bool = False,
110110
blocksize: tuple[int, int] | None = None,
@@ -116,7 +116,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
116116
self,
117117
/,
118118
arg1: onp.ToComplexND,
119-
shape: ToShape2d | None,
119+
shape: ToShape2D | None,
120120
copy: bool = False,
121121
blocksize: tuple[int, int] | None = None,
122122
*,
@@ -127,7 +127,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
127127
self,
128128
/,
129129
arg1: onp.ToComplexND,
130-
shape: ToShape2d | None = None,
130+
shape: ToShape2D | None = None,
131131
*,
132132
copy: bool = False,
133133
blocksize: tuple[int, int] | None = None,

‎scipy-stubs/sparse/_compressed.pyi

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,22 @@ import optype.numpy as onp
1010
from ._base import _spbase
1111
from ._data import _data_matrix, _minmax_mixin
1212
from ._index import IndexMixin
13-
from ._typing import (
14-
Index1D,
15-
Int,
16-
Scalar,
17-
Shape,
18-
ToDType,
19-
ToDTypeBool,
20-
ToDTypeComplex,
21-
ToDTypeFloat,
22-
ToDTypeInt,
23-
ToShape1d,
24-
ToShape1dNd,
25-
ToShape2d,
26-
)
13+
from ._typing import Index1D, Integer, Numeric, ToShape1D, ToShape2D, ToShapeMin1D
2714

2815
__all__: list[str] = []
2916

3017
_T = TypeVar("_T")
31-
_SCT = TypeVar("_SCT", bound=Scalar, default=Any)
32-
_SCT0 = TypeVar("_SCT0", bound=Scalar)
33-
_ShapeT_co = TypeVar("_ShapeT_co", bound=Shape, default=Shape, covariant=True)
18+
_SCT = TypeVar("_SCT", bound=Numeric, default=Any)
19+
_SCT0 = TypeVar("_SCT0", bound=Numeric)
20+
_ShapeT_co = TypeVar("_ShapeT_co", bound=onp.AtLeast1D, default=onp.AtLeast1D, covariant=True)
3421

3522
_ToMatrix: TypeAlias = _spbase[_SCT] | onp.CanArrayND[_SCT] | Sequence[onp.CanArrayND[_SCT]] | _ToMatrixPy[_SCT]
3623
_ToMatrixPy: TypeAlias = Sequence[_T] | Sequence[Sequence[_T]]
3724

38-
_ToData2B: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Int]] # bsr
39-
_ToData2C: TypeAlias = tuple[onp.ArrayND[_SCT], tuple[onp.ArrayND[Int], onp.ArrayND[Int]]] # csc, csr
25+
_ToData2B: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Integer]] # bsr
26+
_ToData2C: TypeAlias = tuple[onp.ArrayND[_SCT], tuple[onp.ArrayND[Integer], onp.ArrayND[Integer]]] # csc, csr
4027
_ToData2: TypeAlias = _ToData2B[_SCT] | _ToData2C[_SCT]
41-
_ToData3: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Int], onp.ArrayND[Int]]
28+
_ToData3: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Integer], onp.ArrayND[Integer]]
4229
_ToData: TypeAlias = _ToData2[_SCT] | _ToData3[_SCT]
4330

4431
###
@@ -49,7 +36,7 @@ class _cs_matrix(
4936
IndexMixin[_SCT, _ShapeT_co],
5037
Generic[_SCT, _ShapeT_co],
5138
):
52-
data: onp.Array[Any, _SCT] # the `Any` shape is needed for `numpy<2.1`
39+
data: onp.ArrayND[_SCT]
5340
indices: Index1D
5441
indptr: Index1D
5542

@@ -76,7 +63,7 @@ class _cs_matrix(
7663
/,
7764
arg1: _spbase[_SCT, _ShapeT_co] | onp.CanArrayND[_SCT, _ShapeT_co],
7865
shape: _ShapeT_co | None = None,
79-
dtype: ToDType[_SCT] | None = None,
66+
dtype: onp.ToDType[_SCT] | None = None,
8067
copy: bool = False,
8168
*,
8269
maxprint: int | None = None,
@@ -86,8 +73,8 @@ class _cs_matrix(
8673
self: _cs_matrix[_SCT0, tuple[int]],
8774
/,
8875
arg1: Sequence[_SCT0],
89-
shape: ToShape1d | None = None,
90-
dtype: ToDType[_SCT0] | None = None,
76+
shape: ToShape1D | None = None,
77+
dtype: onp.ToDType[_SCT0] | None = None,
9178
copy: bool = False,
9279
*,
9380
maxprint: int | None = None,
@@ -97,8 +84,8 @@ class _cs_matrix(
9784
self: _cs_matrix[_SCT0, tuple[int, int]],
9885
/,
9986
arg1: Sequence[Sequence[_SCT0]] | Sequence[onp.CanArrayND[_SCT0]], # assumes max. 2-d
100-
shape: ToShape2d | None = None,
101-
dtype: ToDType[_SCT0] | None = None,
87+
shape: ToShape2D | None = None,
88+
dtype: onp.ToDType[_SCT0] | None = None,
10289
copy: bool = False,
10390
*,
10491
maxprint: int | None = None,
@@ -108,8 +95,8 @@ class _cs_matrix(
10895
self,
10996
/,
11097
arg1: _ToMatrix[_SCT] | _ToData[_SCT],
111-
shape: ToShape1dNd | None = None,
112-
dtype: ToDType[_SCT] | None = None,
98+
shape: ToShapeMin1D | None = None,
99+
dtype: onp.ToDType[_SCT] | None = None,
113100
copy: bool = False,
114101
*,
115102
maxprint: int | None = None,
@@ -118,9 +105,9 @@ class _cs_matrix(
118105
def __init__(
119106
self: _cs_matrix[np.float64, tuple[int]],
120107
/,
121-
arg1: ToShape1d,
122-
shape: ToShape1d | None = None,
123-
dtype: ToDTypeFloat | None = None,
108+
arg1: ToShape1D,
109+
shape: ToShape1D | None = None,
110+
dtype: onp.AnyFloat64DType | None = None,
124111
copy: bool = False,
125112
*,
126113
maxprint: int | None = None,
@@ -129,9 +116,9 @@ class _cs_matrix(
129116
def __init__(
130117
self: _cs_matrix[np.float64, tuple[int, int]],
131118
/,
132-
arg1: ToShape2d,
133-
shape: ToShape2d | None = None,
134-
dtype: ToDTypeFloat | None = None,
119+
arg1: ToShape2D,
120+
shape: ToShape2D | None = None,
121+
dtype: onp.AnyFloat64DType | None = None,
135122
copy: bool = False,
136123
*,
137124
maxprint: int | None = None,
@@ -141,8 +128,8 @@ class _cs_matrix(
141128
self: _cs_matrix[np.bool_, tuple[int]],
142129
/,
143130
arg1: Sequence[bool],
144-
shape: ToShape1d | None = None,
145-
dtype: ToDTypeBool | None = None,
131+
shape: ToShape1D | None = None,
132+
dtype: onp.AnyBoolDType | None = None,
146133
copy: bool = False,
147134
*,
148135
maxprint: int | None = None,
@@ -152,8 +139,8 @@ class _cs_matrix(
152139
self: _cs_matrix[np.bool_, tuple[int, int]],
153140
/,
154141
arg1: Sequence[Sequence[bool]],
155-
shape: ToShape2d | None = None,
156-
dtype: ToDTypeBool | None = None,
142+
shape: ToShape2D | None = None,
143+
dtype: onp.AnyBoolDType | None = None,
157144
copy: bool = False,
158145
*,
159146
maxprint: int | None = None,
@@ -163,8 +150,8 @@ class _cs_matrix(
163150
self: _cs_matrix[np.int_, tuple[int]],
164151
/,
165152
arg1: Sequence[op.JustInt],
166-
shape: ToShape1dNd | None = None,
167-
dtype: ToDTypeInt | None = None,
153+
shape: ToShapeMin1D | None = None,
154+
dtype: onp.AnyIntDType | None = None,
168155
copy: bool = False,
169156
*,
170157
maxprint: int | None = None,
@@ -174,8 +161,8 @@ class _cs_matrix(
174161
self: _cs_matrix[np.int_, tuple[int, int]],
175162
/,
176163
arg1: Sequence[Sequence[op.JustInt]],
177-
shape: ToShape1dNd | None = None,
178-
dtype: ToDTypeInt | None = None,
164+
shape: ToShapeMin1D | None = None,
165+
dtype: onp.AnyIntDType | None = None,
179166
copy: bool = False,
180167
*,
181168
maxprint: int | None = None,
@@ -185,8 +172,8 @@ class _cs_matrix(
185172
self: _cs_matrix[np.float64, tuple[int]],
186173
/,
187174
arg1: Sequence[op.JustFloat],
188-
shape: ToShape1dNd | None = None,
189-
dtype: ToDTypeFloat | None = None,
175+
shape: ToShapeMin1D | None = None,
176+
dtype: onp.AnyFloat64DType | None = None,
190177
copy: bool = False,
191178
*,
192179
maxprint: int | None = None,
@@ -196,8 +183,8 @@ class _cs_matrix(
196183
self: _cs_matrix[np.float64, tuple[int, int]],
197184
/,
198185
arg1: Sequence[Sequence[op.JustFloat]],
199-
shape: ToShape1dNd | None = None,
200-
dtype: ToDTypeFloat | None = None,
186+
shape: ToShapeMin1D | None = None,
187+
dtype: onp.AnyFloat64DType | None = None,
201188
copy: bool = False,
202189
*,
203190
maxprint: int | None = None,
@@ -207,8 +194,8 @@ class _cs_matrix(
207194
self: _cs_matrix[np.complex128, tuple[int]],
208195
/,
209196
arg1: Sequence[op.JustComplex],
210-
shape: ToShape1dNd | None = None,
211-
dtype: ToDTypeComplex | None = None,
197+
shape: ToShapeMin1D | None = None,
198+
dtype: onp.AnyComplex128DType | None = None,
212199
copy: bool = False,
213200
*,
214201
maxprint: int | None = None,
@@ -218,8 +205,8 @@ class _cs_matrix(
218205
self: _cs_matrix[np.complex128, tuple[int, int]],
219206
/,
220207
arg1: Sequence[Sequence[op.JustComplex]],
221-
shape: ToShape1dNd | None = None,
222-
dtype: ToDTypeComplex | None = None,
208+
shape: ToShapeMin1D | None = None,
209+
dtype: onp.AnyComplex128DType | None = None,
223210
copy: bool = False,
224211
*,
225212
maxprint: int | None = None,
@@ -229,8 +216,8 @@ class _cs_matrix(
229216
self: _cs_matrix[_SCT0, tuple[int]],
230217
/,
231218
arg1: onp.ToComplexStrict1D,
232-
shape: ToShape1d | None,
233-
dtype: ToDType[_SCT0],
219+
shape: ToShape1D | None,
220+
dtype: onp.ToDType[_SCT0],
234221
copy: bool = False,
235222
*,
236223
maxprint: int | None = None,
@@ -240,9 +227,9 @@ class _cs_matrix(
240227
self: _cs_matrix[_SCT0, tuple[int]],
241228
/,
242229
arg1: onp.ToComplexStrict1D,
243-
shape: ToShape1d | None = None,
230+
shape: ToShape1D | None = None,
244231
*,
245-
dtype: ToDType[_SCT0],
232+
dtype: onp.ToDType[_SCT0],
246233
copy: bool = False,
247234
maxprint: int | None = None,
248235
) -> None: ...
@@ -251,8 +238,8 @@ class _cs_matrix(
251238
self: _cs_matrix[_SCT0, tuple[int, int]],
252239
/,
253240
arg1: onp.ToComplexStrict2D,
254-
shape: ToShape2d | None,
255-
dtype: ToDType[_SCT0],
241+
shape: ToShape2D | None,
242+
dtype: onp.ToDType[_SCT0],
256243
copy: bool = False,
257244
*,
258245
maxprint: int | None = None,
@@ -262,9 +249,9 @@ class _cs_matrix(
262249
self: _cs_matrix[_SCT0, tuple[int, int]],
263250
/,
264251
arg1: onp.ToComplexStrict2D,
265-
shape: ToShape2d | None = None,
252+
shape: ToShape2D | None = None,
266253
*,
267-
dtype: ToDType[_SCT0],
254+
dtype: onp.ToDType[_SCT0],
268255
copy: bool = False,
269256
maxprint: int | None = None,
270257
) -> None: ...
@@ -273,7 +260,7 @@ class _cs_matrix(
273260
self,
274261
/,
275262
arg1: onp.ToComplex1D | onp.ToComplex2D,
276-
shape: ToShape1dNd | None = None,
263+
shape: ToShapeMin1D | None = None,
277264
dtype: npt.DTypeLike | None = ...,
278265
copy: bool = False,
279266
*,

0 commit comments

Comments
 (0)