Skip to content

Commit 25fb2de

Browse files
Bas van BeekBvB93
authored andcommitted
ENH: Add annotations for np.matrix
1 parent 8c9cf79 commit 25fb2de

File tree

3 files changed

+142
-52
lines changed

3 files changed

+142
-52
lines changed

numpy/__init__.pyi

Lines changed: 120 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -724,53 +724,6 @@ class chararray(ndarray[_ShapeType, _DType_co]):
724724
def isnumeric(self): ...
725725
def isdecimal(self): ...
726726

727-
class matrix(ndarray[_ShapeType, _DType_co]):
728-
def __new__(
729-
subtype,
730-
data: Any,
731-
dtype: Any = ...,
732-
copy: Any = ...,
733-
) -> Any: ...
734-
def __array_finalize__(self, obj): ...
735-
def __getitem__(self, index): ...
736-
def __mul__(self, other): ...
737-
def __rmul__(self, other): ...
738-
def __imul__(self, other): ...
739-
def __pow__(self, other): ...
740-
def __ipow__(self, other): ...
741-
def __rpow__(self, other): ...
742-
def tolist(self): ...
743-
def sum(self, axis=..., dtype=..., out=...): ...
744-
def squeeze(self, axis=...): ...
745-
def flatten(self, order=...): ...
746-
def mean(self, axis=..., dtype=..., out=...): ...
747-
def std(self, axis=..., dtype=..., out=..., ddof=...): ...
748-
def var(self, axis=..., dtype=..., out=..., ddof=...): ...
749-
def prod(self, axis=..., dtype=..., out=...): ...
750-
def any(self, axis=..., out=...): ...
751-
def all(self, axis=..., out=...): ...
752-
def max(self, axis=..., out=...): ...
753-
def argmax(self, axis=..., out=...): ...
754-
def min(self, axis=..., out=...): ...
755-
def argmin(self, axis=..., out=...): ...
756-
def ptp(self, axis=..., out=...): ...
757-
def ravel(self, order=...): ...
758-
@property
759-
def T(self): ...
760-
@property
761-
def I(self): ...
762-
@property
763-
def A(self): ...
764-
@property
765-
def A1(self): ...
766-
@property
767-
def H(self): ...
768-
def getT(self): ...
769-
def getA(self): ...
770-
def getA1(self): ...
771-
def getH(self): ...
772-
def getI(self): ...
773-
774727
# Some of these are aliases; others are wrappers with an identical signature
775728
round = around
776729
round_ = around
@@ -3916,3 +3869,123 @@ class poly1d:
39163869
m: SupportsInt | SupportsIndex = ...,
39173870
k: None | _ArrayLikeComplex_co | _ArrayLikeObject_co = ...,
39183871
) -> poly1d: ...
3872+
3873+
class matrix(ndarray[_ShapeType, _DType_co]):
3874+
def __new__(
3875+
subtype,
3876+
data: ArrayLike,
3877+
dtype: DTypeLike = ...,
3878+
copy: bool = ...,
3879+
) -> matrix[Any, Any]: ...
3880+
def __array_finalize__(self, obj: NDArray[Any]) -> None: ...
3881+
def __getitem__(self, index): ... # TODO
3882+
def __mul__(self, other: ArrayLike) -> matrix[Any, Any]: ...
3883+
def __rmul__(self, other: ArrayLike) -> matrix[Any, Any]: ...
3884+
def __imul__(self, other: ArrayLike) -> matrix[_ShapeType, _DType_co]: ...
3885+
def __pow__(self, other: ArrayLike) -> matrix[Any, Any]: ...
3886+
def __ipow__(self, other: ArrayLike) -> matrix[_ShapeType, _DType_co]: ...
3887+
3888+
@overload
3889+
def sum(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
3890+
@overload
3891+
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3892+
@overload
3893+
def sum(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3894+
3895+
@overload
3896+
def mean(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
3897+
@overload
3898+
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3899+
@overload
3900+
def mean(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3901+
3902+
@overload
3903+
def std(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
3904+
@overload
3905+
def std(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
3906+
@overload
3907+
def std(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...
3908+
3909+
@overload
3910+
def var(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
3911+
@overload
3912+
def var(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
3913+
@overload
3914+
def var(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...
3915+
3916+
@overload
3917+
def prod(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
3918+
@overload
3919+
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3920+
@overload
3921+
def prod(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3922+
3923+
@overload
3924+
def any(self, axis: None = ..., out: None = ...) -> bool_: ...
3925+
@overload
3926+
def any(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[bool_]]: ...
3927+
@overload
3928+
def any(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3929+
3930+
@overload
3931+
def all(self, axis: None = ..., out: None = ...) -> bool_: ...
3932+
@overload
3933+
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[bool_]]: ...
3934+
@overload
3935+
def all(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3936+
3937+
@overload
3938+
def max(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
3939+
@overload
3940+
def max(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
3941+
@overload
3942+
def max(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3943+
3944+
@overload
3945+
def min(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
3946+
@overload
3947+
def min(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
3948+
@overload
3949+
def min(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3950+
3951+
@overload
3952+
def argmax(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
3953+
@overload
3954+
def argmax(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
3955+
@overload
3956+
def argmax(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3957+
3958+
@overload
3959+
def argmin(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
3960+
@overload
3961+
def argmin(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
3962+
@overload
3963+
def argmin(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3964+
3965+
@overload
3966+
def ptp(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
3967+
@overload
3968+
def ptp(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
3969+
@overload
3970+
def ptp(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
3971+
3972+
def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[Any, _DType_co]: ...
3973+
def tolist(self: matrix[Any, dtype[_SupportsItem[_T]]]) -> List[List[_T]]: ... # type: ignore[typevar]
3974+
def ravel(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
3975+
def flatten(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
3976+
3977+
@property
3978+
def T(self) -> matrix[Any, _DType_co]: ...
3979+
@property
3980+
def I(self) -> matrix[Any, Any]: ...
3981+
@property
3982+
def A(self) -> ndarray[_ShapeType, _DType_co]: ...
3983+
@property
3984+
def A1(self) -> ndarray[Any, _DType_co]: ...
3985+
@property
3986+
def H(self) -> matrix[Any, _DType_co]: ...
3987+
def getT(self) -> matrix[Any, _DType_co]: ...
3988+
def getI(self) -> matrix[Any, Any]: ...
3989+
def getA(self) -> ndarray[_ShapeType, _DType_co]: ...
3990+
def getA1(self) -> ndarray[Any, _DType_co]: ...
3991+
def getH(self) -> matrix[Any, _DType_co]: ...

numpy/matrixlib/__init__.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
from typing import Any, List
1+
from typing import List
22

33
from numpy._pytesttester import PytestTester
44

55
from numpy import (
66
matrix as matrix,
77
)
88

9+
from numpy.matrixlib.defmatrix import (
10+
bmat as bmat,
11+
mat as mat,
12+
asmatrix as asmatrix,
13+
)
14+
915
__all__: List[str]
1016
__path__: List[str]
1117
test: PytestTester
12-
13-
def bmat(obj, ldict=..., gdict=...): ...
14-
def asmatrix(data, dtype=...): ...
15-
mat = asmatrix

numpy/matrixlib/defmatrix.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List, Any, Sequence, Mapping
2+
from numpy import matrix as matrix
3+
from numpy.typing import ArrayLike, DTypeLike, NDArray
4+
5+
__all__: List[str]
6+
7+
def bmat(
8+
obj: str | Sequence[ArrayLike] | NDArray[Any],
9+
ldict: None | Mapping[str, Any] = ...,
10+
gdict: None | Mapping[str, Any] = ...,
11+
) -> matrix[Any, Any]: ...
12+
13+
def asmatrix(data: ArrayLike, dtype: DTypeLike = ...) -> matrix[Any, Any]: ...
14+
15+
mat = asmatrix

0 commit comments

Comments
 (0)