Skip to content

Commit 2f84514

Browse files
committed
🎨 refactor from _numtype import in matrixlib
1 parent 8f48bfd commit 2f84514

1 file changed

Lines changed: 59 additions & 88 deletions

File tree

‎src/numpy-stubs/matrixlib/defmatrix.pyi‎

Lines changed: 59 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,8 @@ from types import EllipsisType
44
from typing import Any, ClassVar, SupportsIndex as CanIndex, TypeAlias, overload
55
from typing_extensions import Self, TypeVar, override
66

7+
import _numtype as _nt
78
import numpy as np
8-
from _numtype import (
9-
Array,
10-
JustBytes,
11-
JustComplex,
12-
JustFloat,
13-
JustInt,
14-
JustStr,
15-
Matrix,
16-
Sequence3ND,
17-
ToBool_nd,
18-
ToBytes_nd,
19-
ToComplex128_nd,
20-
ToFloat64_nd,
21-
ToGeneric_3nd,
22-
ToGeneric_nd,
23-
ToInt_nd,
24-
ToInteger_1nd,
25-
ToObject_nd,
26-
ToStr_nd,
27-
_ToArray_1nd,
28-
_ToArray_nd,
29-
)
309
from numpy import _CanItem, _OrderKACF # noqa: ICN003
3110
from numpy._typing import ArrayLike, DTypeLike, _ArrayLikeInt_co, _DTypeLike
3211

@@ -35,14 +14,14 @@ __all__ = ["asmatrix", "bmat", "matrix"]
3514
###
3615

3716
_T = TypeVar("_T")
38-
_ArrayT = TypeVar("_ArrayT", bound=Array)
17+
_ArrayT = TypeVar("_ArrayT", bound=_nt.Array)
3918
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
4019
_ShapeT_co = TypeVar("_ShapeT_co", bound=_2D, default=_2D, covariant=True)
4120
_DTypeT_co = TypeVar("_DTypeT_co", bound=np.dtype, default=np.dtype, covariant=True)
4221

4322
_2D: TypeAlias = tuple[int, int]
4423

45-
_ToIndex1: TypeAlias = slice | EllipsisType | ToInteger_1nd | None
24+
_ToIndex1: TypeAlias = slice | EllipsisType | _nt.ToInteger_1nd | None
4625
_ToIndex2: TypeAlias = tuple[_ToIndex1, _ToIndex1 | CanIndex] | tuple[_ToIndex1 | CanIndex, _ToIndex1]
4726

4827
_ToAxis: TypeAlias = CanIndex | tuple[()] | tuple[CanIndex] | tuple[CanIndex, CanIndex]
@@ -53,39 +32,39 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
5332
__array_priority__: ClassVar[float] = 10.0 # pyright: ignore[reportIncompatibleMethodOverride]
5433

5534
#
56-
def __new__(subtype, data: ArrayLike, dtype: DTypeLike | None = None, copy: bool = ...) -> Matrix: ...
35+
def __new__(subtype, data: ArrayLike, dtype: DTypeLike | None = None, copy: bool = ...) -> _nt.Matrix: ...
5736

5837
#
5938
@overload # type: ignore[override]
6039
def __getitem__(self, key: CanIndex | _ArrayLikeInt_co | tuple[CanIndex | _ArrayLikeInt_co, ...], /) -> Any: ...
6140
@overload
6241
def __getitem__(self, key: _ToIndex1 | _ToIndex2, /) -> matrix[_2D, _DTypeT_co]: ...
6342
@overload
64-
def __getitem__(self: Array[np.void], key: str, /) -> matrix[_ShapeT_co, np.dtype]: ...
43+
def __getitem__(self: _nt.Array[np.void], key: str, /) -> matrix[_ShapeT_co, np.dtype]: ...
6544
@overload
66-
def __getitem__(self: Array[np.void], key: list[str], /) -> matrix[_ShapeT_co, np.dtype[np.void]]: ... # pyright: ignore[reportIncompatibleMethodOverride]
45+
def __getitem__(self: _nt.Array[np.void], key: list[str], /) -> matrix[_ShapeT_co, np.dtype[np.void]]: ... # pyright: ignore[reportIncompatibleMethodOverride]
6746

6847
#
6948
@override
70-
def __mul__(self, other: ArrayLike, /) -> Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
49+
def __mul__(self, other: ArrayLike, /) -> _nt.Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
7150
@override
72-
def __rmul__(self, other: ArrayLike, /) -> Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
51+
def __rmul__(self, other: ArrayLike, /) -> _nt.Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
7352
@override
7453
def __imul__(self, other: Incomplete, /) -> Self: ... # type: ignore[override]
7554

7655
#
7756
@override
78-
def __pow__(self, other: ArrayLike, /) -> Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
57+
def __pow__(self, other: ArrayLike, /) -> _nt.Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
7958
@override
80-
def __rpow__(self, other: ArrayLike, /) -> Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
59+
def __rpow__(self, other: ArrayLike, /) -> _nt.Matrix[Incomplete]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
8160
@override
8261
def __ipow__(self, other: Incomplete, /) -> Self: ... # type: ignore[override]
8362

8463
#
8564
@overload # type: ignore[override]
8665
def sum(self, /, axis: None = None, dtype: DTypeLike | None = None, out: None = None) -> Any: ...
8766
@overload
88-
def sum(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None) -> Matrix: ...
67+
def sum(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None) -> _nt.Matrix: ...
8968
@overload
9069
def sum(self, /, axis: _ToAxis | None, dtype: DTypeLike, out: _ArrayT) -> _ArrayT: ...
9170
@overload
@@ -95,7 +74,7 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
9574
@overload # type: ignore[override]
9675
def mean(self, /, axis: None = None, dtype: DTypeLike | None = None, out: None = None) -> Any: ...
9776
@overload
98-
def mean(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None) -> Matrix: ...
77+
def mean(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None) -> _nt.Matrix: ...
9978
@overload
10079
def mean(self, /, axis: _ToAxis | None, dtype: DTypeLike | None, out: _ArrayT) -> _ArrayT: ...
10180
@overload
@@ -105,43 +84,35 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
10584
@overload # type: ignore[override]
10685
def std(self, /, axis: None = None, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> Any: ...
10786
@overload
108-
def std(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> Matrix: ...
87+
def std(
88+
self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0
89+
) -> _nt.Matrix: ...
10990
@overload
11091
def std(self, /, axis: _ToAxis | None, dtype: DTypeLike | None, out: _ArrayT, ddof: float = 0) -> _ArrayT: ...
11192
@overload
11293
def std( # pyright: ignore[reportIncompatibleMethodOverride]
113-
self,
114-
/,
115-
axis: _ToAxis | None = None,
116-
dtype: DTypeLike | None = None,
117-
*,
118-
out: _ArrayT,
119-
ddof: float = 0,
94+
self, /, axis: _ToAxis | None = None, dtype: DTypeLike | None = None, *, out: _ArrayT, ddof: float = 0
12095
) -> _ArrayT: ...
12196

12297
#
12398
@overload # type: ignore[override]
12499
def var(self, /, axis: None = None, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> Any: ...
125100
@overload
126-
def var(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0) -> Matrix: ...
101+
def var(
102+
self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None, ddof: float = 0
103+
) -> _nt.Matrix: ...
127104
@overload
128105
def var(self, /, axis: _ToAxis | None, dtype: DTypeLike | None, out: _ArrayT, ddof: float = 0) -> _ArrayT: ...
129106
@overload
130107
def var( # pyright: ignore[reportIncompatibleMethodOverride]
131-
self,
132-
/,
133-
axis: _ToAxis | None = None,
134-
dtype: DTypeLike | None = None,
135-
*,
136-
out: _ArrayT,
137-
ddof: float = 0,
108+
self, /, axis: _ToAxis | None = None, dtype: DTypeLike | None = None, *, out: _ArrayT, ddof: float = 0
138109
) -> _ArrayT: ...
139110

140111
#
141112
@overload # type: ignore[override]
142113
def prod(self, /, axis: None = None, dtype: DTypeLike | None = None, out: None = None) -> Any: ...
143114
@overload
144-
def prod(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None) -> Matrix: ...
115+
def prod(self, /, axis: _ToAxis, dtype: DTypeLike | None = None, out: None = None) -> _nt.Matrix: ...
145116
@overload
146117
def prod(self, /, axis: _ToAxis | None, dtype: DTypeLike | None, out: _ArrayT) -> _ArrayT: ...
147118
@overload
@@ -151,7 +122,7 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
151122
@overload # type: ignore[override]
152123
def any(self, /, axis: None = None, out: None = None) -> np.bool: ...
153124
@overload
154-
def any(self, /, axis: _ToAxis, out: None = None) -> Matrix[np.bool]: ...
125+
def any(self, /, axis: _ToAxis, out: None = None) -> _nt.Matrix[np.bool]: ...
155126
@overload
156127
def any(self, /, axis: _ToAxis | None, out: _ArrayT) -> _ArrayT: ...
157128
@overload
@@ -161,15 +132,15 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
161132
@overload # type: ignore[override]
162133
def all(self, /, axis: None = None, out: None = None) -> np.bool: ...
163134
@overload
164-
def all(self, /, axis: _ToAxis, out: None = None) -> Matrix[np.bool]: ...
135+
def all(self, /, axis: _ToAxis, out: None = None) -> _nt.Matrix[np.bool]: ...
165136
@overload
166137
def all(self, /, axis: _ToAxis | None, out: _ArrayT) -> _ArrayT: ...
167138
@overload
168139
def all(self, /, axis: _ToAxis | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
169140

170141
#
171142
@overload # type: ignore[override]
172-
def max(self: Array[_ScalarT], /, axis: None = None, out: None = None) -> _ScalarT: ...
143+
def max(self: _nt.Array[_ScalarT], /, axis: None = None, out: None = None) -> _ScalarT: ...
173144
@overload
174145
def max(self, /, axis: _ToAxis, out: None = None) -> matrix[_2D, _DTypeT_co]: ...
175146
@overload
@@ -179,7 +150,7 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
179150

180151
#
181152
@overload # type: ignore[override]
182-
def min(self: Array[_ScalarT], /, axis: None = None, out: None = None) -> _ScalarT: ...
153+
def min(self: _nt.Array[_ScalarT], /, axis: None = None, out: None = None) -> _ScalarT: ...
183154
@overload
184155
def min(self, /, axis: _ToAxis, out: None = None) -> matrix[_2D, _DTypeT_co]: ...
185156
@overload
@@ -189,27 +160,27 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
189160

190161
#
191162
@overload # type: ignore[override]
192-
def argmax(self: Array[_ScalarT], /, axis: None = None, out: None = None) -> np.intp: ...
163+
def argmax(self: _nt.Array[_ScalarT], /, axis: None = None, out: None = None) -> np.intp: ...
193164
@overload
194-
def argmax(self, /, axis: _ToAxis, out: None = None) -> Matrix[np.intp]: ...
165+
def argmax(self, /, axis: _ToAxis, out: None = None) -> _nt.Matrix[np.intp]: ...
195166
@overload
196167
def argmax(self, /, axis: _ToAxis | None, out: _ArrayT) -> _ArrayT: ...
197168
@overload
198169
def argmax(self, /, axis: _ToAxis | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
199170

200171
#
201172
@overload # type: ignore[override]
202-
def argmin(self: Array[_ScalarT], /, axis: None = None, out: None = None) -> np.intp: ...
173+
def argmin(self: _nt.Array[_ScalarT], /, axis: None = None, out: None = None) -> np.intp: ...
203174
@overload
204-
def argmin(self, /, axis: _ToAxis, out: None = None) -> Matrix[np.intp]: ...
175+
def argmin(self, /, axis: _ToAxis, out: None = None) -> _nt.Matrix[np.intp]: ...
205176
@overload
206177
def argmin(self, /, axis: _ToAxis | None, out: _ArrayT) -> _ArrayT: ...
207178
@overload
208179
def argmin(self, /, axis: _ToAxis | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
209180

210181
#
211182
@overload # type: ignore[override]
212-
def ptp(self: Array[_ScalarT], /, axis: None = None, out: None = None) -> _ScalarT: ...
183+
def ptp(self: _nt.Array[_ScalarT], /, axis: None = None, out: None = None) -> _ScalarT: ...
213184
@overload
214185
def ptp(self, /, axis: _ToAxis, out: None = None) -> matrix[_2D, _DTypeT_co]: ...
215186
@overload
@@ -242,8 +213,8 @@ class matrix(np.ndarray[_ShapeT_co, _DTypeT_co]):
242213

243214
#
244215
@property
245-
def I(self) -> Matrix: ...
246-
def getI(self) -> Matrix: ...
216+
def I(self) -> _nt.Matrix: ...
217+
def getI(self) -> _nt.Matrix: ...
247218

248219
#
249220
@property
@@ -261,74 +232,74 @@ def bmat(
261232
obj: str,
262233
ldict: Mapping[str, Any] | None = None,
263234
gdict: Mapping[str, Any] | None = None,
264-
) -> Matrix: ...
235+
) -> _nt.Matrix: ...
265236
@overload
266237
def bmat(
267-
obj: _ToArray_1nd[_ScalarT],
238+
obj: _nt._ToArray_1nd[_ScalarT],
268239
ldict: Mapping[str, Any] | None = None,
269240
gdict: Mapping[str, Any] | None = None,
270-
) -> Matrix[_ScalarT]: ...
241+
) -> _nt.Matrix[_ScalarT]: ...
271242
@overload
272243
def bmat(
273-
obj: Sequence3ND[bool],
244+
obj: _nt.Sequence3ND[bool],
274245
ldict: Mapping[str, Any] | None = None,
275246
gdict: Mapping[str, Any] | None = None,
276-
) -> Matrix[np.bool]: ...
247+
) -> _nt.Matrix[np.bool]: ...
277248
@overload
278249
def bmat(
279-
obj: Sequence3ND[JustInt],
250+
obj: _nt.Sequence3ND[_nt.JustInt],
280251
ldict: Mapping[str, Any] | None = None,
281252
gdict: Mapping[str, Any] | None = None,
282-
) -> Matrix[np.intp]: ...
253+
) -> _nt.Matrix[np.intp]: ...
283254
@overload
284255
def bmat(
285-
obj: Sequence3ND[JustFloat],
256+
obj: _nt.Sequence3ND[_nt.JustFloat],
286257
ldict: Mapping[str, Any] | None = None,
287258
gdict: Mapping[str, Any] | None = None,
288-
) -> Matrix[np.float64]: ...
259+
) -> _nt.Matrix[np.float64]: ...
289260
@overload
290261
def bmat(
291-
obj: Sequence3ND[JustComplex],
262+
obj: _nt.Sequence3ND[_nt.JustComplex],
292263
ldict: Mapping[str, Any] | None = None,
293264
gdict: Mapping[str, Any] | None = None,
294-
) -> Matrix[np.complex128]: ...
265+
) -> _nt.Matrix[np.complex128]: ...
295266
@overload
296267
def bmat(
297-
obj: Sequence3ND[JustBytes],
268+
obj: _nt.Sequence3ND[_nt.JustBytes],
298269
ldict: Mapping[str, Any] | None = None,
299270
gdict: Mapping[str, Any] | None = None,
300-
) -> Matrix[np.bytes_]: ...
271+
) -> _nt.Matrix[np.bytes_]: ...
301272
@overload
302273
def bmat(
303-
obj: Sequence3ND[JustStr],
274+
obj: _nt.Sequence3ND[_nt.JustStr],
304275
ldict: Mapping[str, Any] | None = None,
305276
gdict: Mapping[str, Any] | None = None,
306-
) -> Matrix[np.str_]: ...
277+
) -> _nt.Matrix[np.str_]: ...
307278
@overload
308279
def bmat(
309-
obj: ToGeneric_3nd,
280+
obj: _nt.ToGeneric_3nd,
310281
ldict: Mapping[str, Any] | None = None,
311282
gdict: Mapping[str, Any] | None = None,
312-
) -> Matrix: ...
283+
) -> _nt.Matrix: ...
313284

314285
#
315286
@overload
316-
def asmatrix(data: _ToArray_nd[_ScalarT], dtype: None = None) -> Matrix[_ScalarT]: ... # type: ignore[overload-overlap]
287+
def asmatrix(data: _nt._ToArray_nd[_ScalarT], dtype: None = None) -> _nt.Matrix[_ScalarT]: ... # type: ignore[overload-overlap]
317288
@overload
318-
def asmatrix(data: ToGeneric_nd, dtype: _DTypeLike[_ScalarT]) -> Matrix[_ScalarT]: ...
289+
def asmatrix(data: _nt.ToGeneric_nd, dtype: _DTypeLike[_ScalarT]) -> _nt.Matrix[_ScalarT]: ...
319290
@overload
320-
def asmatrix(data: ToBool_nd, dtype: None = None) -> Matrix[np.bool]: ...
291+
def asmatrix(data: _nt.ToBool_nd, dtype: None = None) -> _nt.Matrix[np.bool]: ...
321292
@overload
322-
def asmatrix(data: ToInt_nd, dtype: None = None) -> Matrix[np.intp]: ...
293+
def asmatrix(data: _nt.ToInt_nd, dtype: None = None) -> _nt.Matrix[np.intp]: ...
323294
@overload
324-
def asmatrix(data: ToFloat64_nd, dtype: None = None) -> Matrix[np.float64]: ...
295+
def asmatrix(data: _nt.ToFloat64_nd, dtype: None = None) -> _nt.Matrix[np.float64]: ...
325296
@overload
326-
def asmatrix(data: ToObject_nd, dtype: None = None) -> Matrix[np.object_]: ...
297+
def asmatrix(data: _nt.ToObject_nd, dtype: None = None) -> _nt.Matrix[np.object_]: ...
327298
@overload
328-
def asmatrix(data: ToComplex128_nd, dtype: None = None) -> Matrix[np.complex128]: ...
299+
def asmatrix(data: _nt.ToComplex128_nd, dtype: None = None) -> _nt.Matrix[np.complex128]: ...
329300
@overload
330-
def asmatrix(data: ToBytes_nd, dtype: None = None) -> Matrix[np.bytes_]: ...
301+
def asmatrix(data: _nt.ToBytes_nd, dtype: None = None) -> _nt.Matrix[np.bytes_]: ...
331302
@overload
332-
def asmatrix(data: ToStr_nd, dtype: None = None) -> Matrix[np.str_]: ...
303+
def asmatrix(data: _nt.ToStr_nd, dtype: None = None) -> _nt.Matrix[np.str_]: ...
333304
@overload
334-
def asmatrix(data: ToGeneric_nd, dtype: DTypeLike | None) -> Matrix: ...
305+
def asmatrix(data: _nt.ToGeneric_nd, dtype: DTypeLike | None) -> _nt.Matrix: ...

0 commit comments

Comments
 (0)