Skip to content

Commit dbfb258

Browse files
authored
Merge pull request #545 from numpy/prefer-rank-over-shape
2 parents ffc0338 + c50cf63 commit dbfb258

File tree

102 files changed

+2786
-3335
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2786
-3335
lines changed

src/_numtype/_rank.pyi

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
from typing import Any, Generic, Protocol, Self, TypeAlias, final, type_check_only
22
from typing_extensions import TypeAliasType, TypeVar
33

4-
from ._shape import (
5-
Shape,
6-
Shape as Shape0ToN,
7-
Shape0,
8-
Shape1,
9-
Shape1N as Shape1ToN,
10-
Shape2,
11-
Shape2N as Shape2ToN,
12-
Shape3,
13-
Shape3N as Shape3ToN,
14-
Shape4,
15-
Shape4N as Shape4ToN,
16-
)
4+
from ._shape import Shape, Shape0, Shape0N, Shape1, Shape1N, Shape2, Shape2N, Shape3, Shape3N, Shape4, Shape4N
175

186
__all__ = [
197
"Broadcasts",
@@ -33,11 +21,11 @@ __all__ = [
3321

3422
###
3523

36-
_Shape0To0: TypeAlias = Shape0
37-
_Shape0To1: TypeAlias = _Shape0To0 | Shape1
38-
_Shape0To2: TypeAlias = _Shape0To1 | Shape2
39-
_Shape0To3: TypeAlias = _Shape0To2 | Shape3
40-
_Shape0To4: TypeAlias = _Shape0To3 | Shape4
24+
_Shape00: TypeAlias = Shape0
25+
_Shape01: TypeAlias = _Shape00 | Shape1
26+
_Shape02: TypeAlias = _Shape01 | Shape2
27+
_Shape03: TypeAlias = _Shape02 | Shape3
28+
_Shape04: TypeAlias = _Shape03 | Shape4
4129

4230
###
4331

@@ -51,38 +39,18 @@ _BroadcastableShape = TypeAliasType(
5139
type_params=(_FromT, _RankT),
5240
)
5341

54-
BroadcastsTo = TypeAliasType(
55-
"BroadcastsTo",
56-
_HasRank[_CanBroadcastTo[_ToT, _RankT]],
57-
type_params=(_ToT, _RankT),
58-
)
59-
Broadcasts = TypeAliasType(
60-
"Broadcasts",
61-
_HasRank[_BroadcastableShape[_FromT, _RankT]],
62-
type_params=(_FromT, _RankT),
63-
)
42+
BroadcastsTo = TypeAliasType("BroadcastsTo", _HasShape[_CanBroadcastTo[_ToT, _RankT]], type_params=(_ToT, _RankT))
43+
Broadcasts = TypeAliasType("Broadcasts", _HasShape[_BroadcastableShape[_FromT, _RankT]], type_params=(_FromT, _RankT))
6444

6545
###
6646

67-
_ShapeT_co = TypeVar(
68-
"_ShapeT_co",
69-
bound=Shape | _HasOwnShape | _CanBroadcastFrom | _CanBroadcastTo,
70-
covariant=True,
71-
)
47+
_ShapeT_co = TypeVar("_ShapeT_co", bound=Shape | _HasOwnShape | _CanBroadcastFrom | _CanBroadcastTo, covariant=True)
7248

7349
@type_check_only
7450
class _HasShape(Protocol[_ShapeT_co]):
7551
@property
7652
def shape(self, /) -> _ShapeT_co: ...
7753

78-
_ShapeT = TypeVar("_ShapeT", bound=Shape)
79-
80-
@final
81-
@type_check_only
82-
class _HasRank(Protocol[_ShapeT_co]):
83-
@property
84-
def shape(self: _HasShape[_ShapeT], /) -> _ShapeT: ...
85-
8654
_FromT_contra = TypeVar("_FromT_contra", default=Any, contravariant=True)
8755
_ToT_contra = TypeVar("_ToT_contra", bound=Shape, default=Any, contravariant=True)
8856
_RankT_co = TypeVar("_RankT_co", bound=Shape, default=Any, covariant=True)
@@ -98,8 +66,8 @@ class _CanBroadcastTo(Protocol[_ToT_contra, _RankT_co]):
9866
def __broadcast_to__(self, to: _ToT_contra, /) -> _RankT_co: ...
9967

10068
# This double shape-type parameter is a sneaky way to annotate a doubly-bound nominal type range,
101-
# e.g. `_HasOwnShape[Shape2ToN, Shape0ToN]` accepts `Shape2ToN`, `Shape1ToN`, and `Shape0ToN`, but
102-
# rejects `Shape3ToN` and `Shape1`. Besides brevity, it also works around several mypy bugs that
69+
# e.g. `_HasOwnShape[Shape2N, Shape0N]` accepts `Shape2N`, `Shape1N`, and `Shape0N`, but
70+
# rejects `Shape3N` and `Shape1`. Besides brevity, it also works around several mypy bugs that
10371
# are related to "unions vs joins".
10472

10573
_OwnShapeT_contra = TypeVar("_OwnShapeT_contra", bound=Shape, default=Any, contravariant=True)
@@ -129,46 +97,46 @@ class _BaseRankM(
12997

13098
@final
13199
@type_check_only
132-
class Rank0(_BaseRankM[_Shape0To0, Shape0ToN, Shape0], tuple[()]): ...
100+
class Rank0(_BaseRankM[_Shape00, Shape0N, Shape0], tuple[()]): ...
133101

134102
@final
135103
@type_check_only
136-
class Rank1(_BaseRankM[_Shape0To1, Shape1ToN, Shape1], tuple[int]): ...
104+
class Rank1(_BaseRankM[_Shape01, Shape1N, Shape1], tuple[int]): ...
137105

138106
@final
139107
@type_check_only
140-
class Rank2(_BaseRankM[_Shape0To2, Shape2ToN, Shape2], tuple[int, int]): ...
108+
class Rank2(_BaseRankM[_Shape02, Shape2N, Shape2], tuple[int, int]): ...
141109

142110
@final
143111
@type_check_only
144-
class Rank3(_BaseRankM[_Shape0To3, Shape3ToN, Shape3], tuple[int, int, int]): ...
112+
class Rank3(_BaseRankM[_Shape03, Shape3N, Shape3], tuple[int, int, int]): ...
145113

146114
@final
147115
@type_check_only
148-
class Rank4(_BaseRankM[_Shape0To4, Shape4ToN, Shape4], tuple[int, int, int, int]): ...
116+
class Rank4(_BaseRankM[_Shape04, Shape4N, Shape4], tuple[int, int, int, int]): ...
149117

150118
# this emulates `AnyOf`, rather than a `Union`.
151119
@type_check_only
152-
class _BaseRankMToN(_BaseRank[Shape0ToN, _OwnShapeT, _OwnShapeT], Generic[_OwnShapeT]): ...
120+
class _BaseRankMToN(_BaseRank[Shape0N, _OwnShapeT, _OwnShapeT], Generic[_OwnShapeT]): ...
153121

154122
@final
155123
@type_check_only
156-
class Rank(_BaseRankMToN[Shape0ToN], tuple[int, ...]): ...
124+
class Rank(_BaseRankMToN[Shape0N], tuple[int, ...]): ...
157125

158126
@final
159127
@type_check_only
160-
class Rank1N(_BaseRankMToN[Shape1ToN], tuple[int, *tuple[int, ...]]): ...
128+
class Rank1N(_BaseRankMToN[Shape1N], tuple[int, *tuple[int, ...]]): ...
161129

162130
@final
163131
@type_check_only
164-
class Rank2N(_BaseRankMToN[Shape2ToN], tuple[int, int, *tuple[int, ...]]): ...
132+
class Rank2N(_BaseRankMToN[Shape2N], tuple[int, int, *tuple[int, ...]]): ...
165133

166134
@final
167135
@type_check_only
168-
class Rank3N(_BaseRankMToN[Shape3ToN], tuple[int, int, int, *tuple[int, ...]]): ...
136+
class Rank3N(_BaseRankMToN[Shape3N], tuple[int, int, int, *tuple[int, ...]]): ...
169137

170138
@final
171139
@type_check_only
172-
class Rank4N(_BaseRankMToN[Shape4ToN], tuple[int, int, int, int, *tuple[int, ...]]): ...
140+
class Rank4N(_BaseRankMToN[Shape4N], tuple[int, int, int, int, *tuple[int, ...]]): ...
173141

174142
Rank0N: TypeAlias = Rank

src/_numtype/_shape.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ __all__ = [
1818

1919
Shape = TypeAliasType("Shape", tuple[int, ...])
2020

21-
ShapeN: TypeAlias = Shape
2221
Shape0 = TypeAliasType("Shape0", tuple[()])
2322
Shape1 = TypeAliasType("Shape1", tuple[int])
2423
Shape2 = TypeAliasType("Shape2", tuple[int, int])
2524
Shape3 = TypeAliasType("Shape3", tuple[int, int, int])
2625
Shape4 = TypeAliasType("Shape4", tuple[int, int, int, int])
26+
ShapeN: TypeAlias = Shape
2727

2828
Shape0N: TypeAlias = Shape
2929
Shape1N = TypeAliasType("Shape1N", tuple[int, *tuple[int, ...]])

src/numpy-stubs/@test/generated/ndarray_abs.pyi

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/numpy-stubs/@test/generated/ndarray_add.pyi

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/numpy-stubs/@test/generated/ndarray_and.pyi

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/numpy-stubs/@test/generated/ndarray_divmod.pyi

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/numpy-stubs/@test/generated/ndarray_floordiv.pyi

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/numpy-stubs/@test/generated/ndarray_invert.pyi

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/numpy-stubs/@test/generated/ndarray_lshift.pyi

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/numpy-stubs/@test/generated/ndarray_matmul.pyi

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)