Skip to content

Commit fb3f2ce

Browse files
authored
♻️ clean up the manual and numpy._typing shape-type usages (#541)
1 parent 00a20dd commit fb3f2ce

25 files changed

+92
-90
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any, TypeAlias, TypeVar, assert_type
1+
from typing import TypeAlias, TypeVar, assert_type
22

3+
import _numtype as _nt
34
import numpy as np
4-
from numpy._typing import _Shape
55

66
m: np.ma.MaskedArray[tuple[int], np.dtype[np.float64]]
77

@@ -13,12 +13,12 @@ assert_type(int(m), int)
1313
assert_type(float(m), float)
1414

1515
_ScalarT_co = TypeVar("_ScalarT_co", bound=np.generic, covariant=True)
16-
MaskedNDArray: TypeAlias = np.ma.MaskedArray[_Shape, np.dtype[_ScalarT_co]]
16+
MaskedNDArray: TypeAlias = _nt.MArray[_ScalarT_co]
1717

1818
class MaskedNDArraySubclass(MaskedNDArray[np.complex128]): ...
1919

2020
MAR_b: MaskedNDArray[np.bool]
2121
MAR_f4: MaskedNDArray[np.float32]
2222
MAR_i8: MaskedNDArray[np.int64]
2323
MAR_subclass: MaskedNDArraySubclass
24-
MAR_1d: np.ma.MaskedArray[tuple[int], np.dtype[Any]]
24+
MAR_1d: _nt.MArray0D

src/numpy-stubs/__init__.pyi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,10 @@ _ArrayT = TypeVar("_ArrayT", bound=NDArray[Any])
627627
_IntegralArrayT = TypeVar("_IntegralArrayT", bound=NDArray[_nt.co_integer | object_])
628628
_NumericArrayT = TypeVar("_NumericArrayT", bound=NDArray[number | timedelta64 | object_])
629629

630-
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
631-
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], covariant=True)
632-
_ShapeT_1nd = TypeVar("_ShapeT_1nd", bound=tuple[int, *tuple[int, ...]])
633-
_1NShapeT = TypeVar("_1NShapeT", bound=tuple[L[1], *tuple[L[1], ...]]) # (1,) | (1, 1) | (1, 1, 1) | ...
630+
_ShapeT = TypeVar("_ShapeT", bound=_nt.Shape)
631+
_ShapeT_co = TypeVar("_ShapeT_co", bound=_nt.Shape, covariant=True)
632+
_ShapeT_1nd = TypeVar("_ShapeT_1nd", bound=_nt.Shape1_)
633+
_1NShapeT = TypeVar("_1NShapeT", bound=tuple[L[1], *tuple[L[1], ...]]) # TODO(jorenham): remove
634634

635635
_ScalarT = TypeVar("_ScalarT", bound=generic)
636636
_SelfScalarT = TypeVar("_SelfScalarT", bound=generic)
@@ -1180,7 +1180,7 @@ class dtype(Generic[_ScalarT_co], metaclass=_DTypeMeta):
11801180
@property
11811181
def num(self) -> _DTypeNum: ...
11821182
@property
1183-
def shape(self) -> tuple[int, ...]: ...
1183+
def shape(self) -> _nt.Shape: ...
11841184
@property
11851185
def ndim(self) -> int: ...
11861186
@property
@@ -2891,15 +2891,15 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
28912891

28922892
#
28932893
@overload
2894-
def tolist(self: _HasShapeAndItem[tuple[()], _T], /) -> _T: ...
2894+
def tolist(self: _HasShapeAndItem[_nt.Shape0, _T], /) -> _T: ...
28952895
@overload
2896-
def tolist(self: _HasShapeAndItem[tuple[int], _T], /) -> list[_T]: ...
2896+
def tolist(self: _HasShapeAndItem[_nt.Shape1, _T], /) -> list[_T]: ...
28972897
@overload
2898-
def tolist(self: _HasShapeAndItem[tuple[int, int], _T], /) -> list[list[_T]]: ...
2898+
def tolist(self: _HasShapeAndItem[_nt.Shape2, _T], /) -> list[list[_T]]: ...
28992899
@overload
2900-
def tolist(self: _HasShapeAndItem[tuple[int, int, int], _T], /) -> list[list[list[_T]]]: ...
2900+
def tolist(self: _HasShapeAndItem[_nt.Shape3, _T], /) -> list[list[list[_T]]]: ...
29012901
@overload
2902-
def tolist(self: _HasShapeAndItem[tuple[int, int, int, int], _T], /) -> list[list[list[list[_T]]]]: ...
2902+
def tolist(self: _HasShapeAndItem[_nt.Shape4, _T], /) -> list[list[list[list[_T]]]]: ...
29032903
@overload
29042904
def tolist(self: _HasShapeAndItem[Any, _T], /) -> Any: ...
29052905

src/numpy-stubs/_core/_exceptions.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from collections.abc import Iterable
22
from typing import Any, Final, overload
33
from typing_extensions import TypeVar
44

5+
import _numtype as _nt
56
import numpy as np
67
from numpy import _CastingKind # noqa: ICN003
78
from numpy._utils import set_module as set_module
@@ -57,9 +58,9 @@ class _UFuncOutputCastingError(_UFuncCastingError):
5758
) -> None: ...
5859

5960
class _ArrayMemoryError(MemoryError):
60-
shape: tuple[int, ...]
61+
shape: _nt.Shape
6162
dtype: np.dtype
62-
def __init__(self, /, shape: tuple[int, ...], dtype: np.dtype) -> None: ...
63+
def __init__(self, /, shape: _nt.Shape, dtype: np.dtype) -> None: ...
6364
@property
6465
def _total_size(self) -> int: ...
6566
@staticmethod

src/numpy-stubs/_core/_multiarray_umath.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ def array(
10251025
subok: bool = False,
10261026
ndmin: L[0] = 0,
10271027
**kwargs: Unpack[_KwargsCL],
1028-
) -> _nt.Array[_ScalarT, tuple[()]]: ...
1028+
) -> _nt.Array0D[_ScalarT]: ...
10291029
@overload
10301030
def array(
10311031
object: _ScalarLike_co,
@@ -1035,7 +1035,7 @@ def array(
10351035
subok: bool = False,
10361036
ndmin: L[0] = 0,
10371037
**kwargs: Unpack[_KwargsCL],
1038-
) -> _nt.Array[Any, tuple[()]]: ...
1038+
) -> _nt.Array0D[Any]: ...
10391039
@overload
10401040
def array(
10411041
object: object,
@@ -1078,14 +1078,14 @@ def asarray(
10781078
dtype: _DTypeLike[_ScalarT],
10791079
order: _OrderKACF = None,
10801080
**kwargs: Unpack[_KwargsDCL],
1081-
) -> _nt.Array[_ScalarT, tuple[()]]: ...
1081+
) -> _nt.Array0D[_ScalarT]: ...
10821082
@overload
10831083
def asarray(
10841084
a: _ScalarLike_co,
10851085
dtype: npt.DTypeLike | None = None,
10861086
order: _OrderKACF = None,
10871087
**kwargs: Unpack[_KwargsDCL],
1088-
) -> _nt.Array[Any, tuple[()]]: ...
1088+
) -> _nt.Array0D[Any]: ...
10891089
@overload
10901090
def asarray(
10911091
a: object,

src/numpy-stubs/_core/fromnumeric.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ __all__ = [
5858

5959
_ArrayT = TypeVar("_ArrayT", bound=np.ndarray[Any, Any])
6060

61+
_ShapeT = TypeVar("_ShapeT", bound=_nt.Shape)
62+
_ShapeT_co = TypeVar("_ShapeT_co", bound=_nt.Shape, covariant=True)
6163
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
62-
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
6364
_NumberT = TypeVar("_NumberT", bound=np.number | np.object_)
64-
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], covariant=True)
6565
_IndT_contra = TypeVar("_IndT_contra", contravariant=True)
6666
_VT_contra = TypeVar("_VT_contra", contravariant=True)
6767
_RT_co = TypeVar("_RT_co", covariant=True)

src/numpy-stubs/_core/memmap.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from typing import (
1414
)
1515
from typing_extensions import TypeVar
1616

17+
import _numtype as _nt
1718
import numpy as np
1819
import numpy.typing as npt
1920
from numpy import _OrderKACF # noqa: ICN003
@@ -23,7 +24,7 @@ __all__ = ["memmap"]
2324

2425
###
2526

26-
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], default=Any, covariant=True)
27+
_ShapeT_co = TypeVar("_ShapeT_co", bound=_nt.Shape, default=Any, covariant=True)
2728
_DType_co = TypeVar("_DType_co", bound=np.dtype, default=np.dtype, covariant=True)
2829
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
2930

src/numpy-stubs/_core/numeric.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ __all__ = [
593593
_T = TypeVar("_T")
594594
_ArrayT = TypeVar("_ArrayT", bound=np.ndarray[Any, Any])
595595
_ArrayT_co = TypeVar("_ArrayT_co", bound=np.ndarray[Any, Any], covariant=True)
596-
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
596+
_ShapeT = TypeVar("_ShapeT", bound=_nt.Shape)
597597
_DTypeT = TypeVar("_DTypeT", bound=np.dtype)
598598
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
599599

src/numpy-stubs/_core/records.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from collections.abc import Iterable, Sequence
33
from typing import IO, Any, ClassVar, Literal as L, Protocol, SupportsIndex, TypeAlias, overload, type_check_only
44
from typing_extensions import Buffer, TypeVar, override
55

6+
import _numtype as _nt
67
import numpy as np
78
from numpy import _ByteOrder, _OrderKACF # noqa: ICN003
89
from numpy._typing import ArrayLike, DTypeLike, NDArray, _ArrayLikeVoid_co, _NestedSequence, _ShapeLike
@@ -22,7 +23,7 @@ __all__ = [
2223
_T = TypeVar("_T")
2324
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
2425
_DTypeT_co = TypeVar("_DTypeT_co", bound=np.dtype, covariant=True)
25-
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], covariant=True)
26+
_ShapeT_co = TypeVar("_ShapeT_co", bound=_nt.Shape, covariant=True)
2627

2728
_RecArray: TypeAlias = recarray[Any, np.dtype[_ScalarT]]
2829

src/numpy-stubs/_core/shape_base.pyi

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from collections.abc import Sequence
2-
from typing import Any, SupportsIndex, TypeAlias, overload
2+
from typing import Any, SupportsIndex, overload
33
from typing_extensions import TypeVar
44

5+
import _numtype as _nt
56
import numpy as np
67
from numpy._typing import ArrayLike, DTypeLike, NDArray, _ArrayLike, _DTypeLike
78

@@ -15,21 +16,17 @@ _SCT1 = TypeVar("_SCT1", bound=np.generic)
1516

1617
_ArrayT = TypeVar("_ArrayT", bound=NDArray[Any])
1718

18-
_Array1T = TypeVar("_Array1T", bound=np.ndarray[_AtLeast1D, np.dtype])
19-
_Array1T0 = TypeVar("_Array1T0", bound=np.ndarray[_AtLeast1D, np.dtype])
20-
_Array1T1 = TypeVar("_Array1T1", bound=np.ndarray[_AtLeast1D, np.dtype])
19+
_Array1T = TypeVar("_Array1T", bound=np.ndarray[_nt.Shape1_, np.dtype])
20+
_Array1T0 = TypeVar("_Array1T0", bound=np.ndarray[_nt.Shape1_, np.dtype])
21+
_Array1T1 = TypeVar("_Array1T1", bound=np.ndarray[_nt.Shape1_, np.dtype])
2122

22-
_Array2T = TypeVar("_Array2T", bound=np.ndarray[_AtLeast2D, np.dtype])
23-
_Array2T0 = TypeVar("_Array2T0", bound=np.ndarray[_AtLeast2D, np.dtype])
24-
_Array2T1 = TypeVar("_Array2T1", bound=np.ndarray[_AtLeast2D, np.dtype])
23+
_Array2T = TypeVar("_Array2T", bound=np.ndarray[_nt.Shape2_, np.dtype])
24+
_Array2T0 = TypeVar("_Array2T0", bound=np.ndarray[_nt.Shape2_, np.dtype])
25+
_Array2T1 = TypeVar("_Array2T1", bound=np.ndarray[_nt.Shape2_, np.dtype])
2526

26-
_Array3T = TypeVar("_Array3T", bound=np.ndarray[_AtLeast3D, np.dtype])
27-
_Array3T0 = TypeVar("_Array3T0", bound=np.ndarray[_AtLeast3D, np.dtype])
28-
_Array3T1 = TypeVar("_Array3T1", bound=np.ndarray[_AtLeast3D, np.dtype])
29-
30-
_AtLeast1D: TypeAlias = tuple[int, *tuple[int, ...]]
31-
_AtLeast2D: TypeAlias = tuple[int, int, *tuple[int, ...]]
32-
_AtLeast3D: TypeAlias = tuple[int, int, int, *tuple[int, ...]]
27+
_Array3T = TypeVar("_Array3T", bound=np.ndarray[_nt.Shape3_, np.dtype])
28+
_Array3T0 = TypeVar("_Array3T0", bound=np.ndarray[_nt.Shape3_, np.dtype])
29+
_Array3T1 = TypeVar("_Array3T1", bound=np.ndarray[_nt.Shape3_, np.dtype])
3330

3431
###
3532

src/numpy-stubs/_typing/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ from ._scalars import (
108108
_UIntLike_co as _UIntLike_co,
109109
_VoidLike_co as _VoidLike_co,
110110
)
111-
from ._shape import _Shape as _Shape, _ShapeLike as _ShapeLike
111+
from ._shape import _ShapeLike as _ShapeLike

0 commit comments

Comments
 (0)