Skip to content

Commit 476bc6b

Browse files
TYP: Force matrix shape type to be 2D
1 parent e093c7e commit 476bc6b

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Editor temporary/working/backup files #
22
#########################################
3-
env/
43
.#*
54
[#]*#
65
*~

doc/release/upcoming_changes/26081.improvement.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ with this change. It is a generic type with type parameters for
55
the shape and the data type. Previously, the shape type parameter could be
66
any value. This change restricts it to a tuple of ints, as one would expect
77
from using ``ndarray.shape``. Further, the shape-type parameter has been
8-
changed from invariant to covariant. This change also applies to subpytes of
9-
``ndarray``, e.g. ``np.ma.MaskedArray``. See the `typing docs <https://typing.readthedocs.io/en/latest/reference/generics.html#variance-of-generic-types>`_
8+
changed from invariant to covariant. This change also applies to the subtypes
9+
of ``ndarray``, e.g. ``numpy.ma.MaskedArray``. See the
10+
`typing docs <https://typing.readthedocs.io/en/latest/reference/generics.html#variance-of-generic-types>`_
1011
for more information.

numpy/__init__.pyi

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,7 @@ _FlexDType = TypeVar("_FlexDType", bound=dtype[flexible])
15021502

15031503
_ShapeType_co = TypeVar("_ShapeType_co", covariant=True, bound=tuple[int, ...])
15041504
_ShapeType2 = TypeVar("_ShapeType2", bound=tuple[int, ...])
1505+
_Shape2DType_co = TypeVar("_Shape2DType_co", covariant=True, bound=tuple[int, int])
15051506
_NumberType = TypeVar("_NumberType", bound=number[Any])
15061507

15071508
if sys.version_info >= (3, 12):
@@ -3925,7 +3926,9 @@ class poly1d:
39253926
k: None | _ArrayLikeComplex_co | _ArrayLikeObject_co = ...,
39263927
) -> poly1d: ...
39273928

3928-
class matrix(ndarray[_ShapeType_co, _DType_co]):
3929+
3930+
3931+
class matrix(ndarray[_Shape2DType_co, _DType_co]):
39293932
__array_priority__: ClassVar[float]
39303933
def __new__(
39313934
subtype,
@@ -3961,13 +3964,13 @@ class matrix(ndarray[_ShapeType_co, _DType_co]):
39613964
@overload
39623965
def __getitem__(self: NDArray[void], key: str, /) -> matrix[Any, dtype[Any]]: ...
39633966
@overload
3964-
def __getitem__(self: NDArray[void], key: list[str], /) -> matrix[_ShapeType_co, dtype[void]]: ...
3967+
def __getitem__(self: NDArray[void], key: list[str], /) -> matrix[_Shape2DType_co, dtype[void]]: ...
39653968

39663969
def __mul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
39673970
def __rmul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3968-
def __imul__(self, other: ArrayLike, /) -> matrix[_ShapeType_co, _DType_co]: ...
3971+
def __imul__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...
39693972
def __pow__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3970-
def __ipow__(self, other: ArrayLike, /) -> matrix[_ShapeType_co, _DType_co]: ...
3973+
def __ipow__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...
39713974

39723975
@overload
39733976
def sum(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
@@ -4063,14 +4066,14 @@ class matrix(ndarray[_ShapeType_co, _DType_co]):
40634066
@property
40644067
def I(self) -> matrix[Any, Any]: ...
40654068
@property
4066-
def A(self) -> ndarray[_ShapeType_co, _DType_co]: ...
4069+
def A(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
40674070
@property
40684071
def A1(self) -> ndarray[Any, _DType_co]: ...
40694072
@property
40704073
def H(self) -> matrix[Any, _DType_co]: ...
40714074
def getT(self) -> matrix[Any, _DType_co]: ...
40724075
def getI(self) -> matrix[Any, Any]: ...
4073-
def getA(self) -> ndarray[_ShapeType_co, _DType_co]: ...
4076+
def getA(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
40744077
def getA1(self) -> ndarray[Any, _DType_co]: ...
40754078
def getH(self) -> matrix[Any, _DType_co]: ...
40764079

0 commit comments

Comments
 (0)