10000 ENH: Make `ndarray` generic w.r.t. its shape and dtype · numpy/numpy@d7a3e02 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7a3e02

Browse files
author
Bas van Beek
committed
ENH: Make ndarray generic w.r.t. its shape and dtype
1 parent ba77419 commit d7a3e02

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

numpy/__init__.pyi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,6 @@ class dtype(Generic[_DTypeScalar]):
913913
@property
914914
def type(self) -> Type[generic]: ...
915915

916-
_DType = dtype # to avoid name conflicts with ndarray.dtype
917-
918916
class _flagsobj:
919917
aligned: bool
920918
updateifcopy: bool
@@ -1441,10 +1439,16 @@ class _ArrayOrScalarCommon:
14411439
keepdims: bool = ...,
14421440
) -> _NdArraySubClass: ...
14431441

1442+
_DType = TypeVar("_DType", bound=dtype[Any])
1443+
1444+
# TODO: Set the `bound` to something more suitable once we
1445+
# have proper shape support
1446+
_ShapeType = TypeVar("_ShapeType", bound=Any)
1447+
14441448
_BufferType = Union[ndarray, bytes, bytearray, memoryview]
14451449
_Casting = Literal["no", "equiv", "safe", "same_kind", "unsafe"]
14461450

1447-
class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
1451+
class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType]):
14481452
@property
14491453
def base(self) -> Optional[ndarray]: ...
14501454
@property
@@ -1469,8 +1473,6 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
14691473
order: _OrderKACF = ...,
14701474
) -> _ArraySelf: ...
14711475
@property
1472-
def dtype(self) -> _DType: ...
1473-
@property
14741476
def ctypes(self) -> _ctypes: ...
14751477
@property
14761478
def shape(self) -> _Shape: ...
@@ -1625,6 +1627,9 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
16251627
def __iand__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
16261628
def __ixor__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
16271629
def __ior__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
1630+
# Keep `dtype` at the bottom to avoid name conflicts with `np.dtype`
1631+
@property
1632+
def dtype(self) -> _DType: ...
16281633

16291634
# NOTE: while `np.generic` is not technically an instance of `ABCMeta`,
16301635
# the `@abstractmethod` decorator is herein used to (forcefully) deny
@@ -1644,8 +1649,6 @@ class generic(_ArrayOrScalarCommon):
16441649
@property
16451650
def base(self) -> None: ...
16461651
@property
1647-
def dtype(self: _ScalarType) -> _DType[_ScalarType]: ...
1648-
@property
16491652
def ndim(self) -> Literal[0]: ...
16501653
@property
16511654
def size(self) -> Literal[1]: ...
@@ -1664,6 +1667,9 @@ class generic(_ArrayOrScalarCommon):
16641667
self: _ScalarType, axis: Union[Literal[0], Tuple[()]] = ...
16651668
) -> _ScalarType: ...
16661669
def transpose(self: _ScalarType, __axes: Tuple[()] = ...) -> _ScalarType: ...
1670+
# Keep `dtype` at the bottom to avoid name conflicts with `np.dtype`
1671+
@property
1672+
def dtype(self: _ScalarType) -> dtype[_ScalarType]: ...
16671673

16681674
class number(generic, Generic[_NBit_co]): # type: ignore
16691675
@property

0 commit comments

Comments
 (0)
0