1
1
from typing import Any , Generic , Protocol , Self , TypeAlias , final , type_check_only
2
2
from typing_extensions import TypeAliasType , TypeVar
3
3
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
17
5
18
6
__all__ = [
19
7
"Broadcasts" ,
@@ -33,11 +21,11 @@ __all__ = [
33
21
34
22
###
35
23
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
41
29
42
30
###
43
31
@@ -51,38 +39,18 @@ _BroadcastableShape = TypeAliasType(
51
39
type_params = (_FromT , _RankT ),
52
40
)
53
41
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 ))
64
44
65
45
###
66
46
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 )
72
48
73
49
@type_check_only
74
50
class _HasShape (Protocol [_ShapeT_co ]):
75
51
@property
76
52
def shape (self , / ) -> _ShapeT_co : ...
77
53
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
-
86
54
_FromT_contra = TypeVar ("_FromT_contra" , default = Any , contravariant = True )
87
55
_ToT_contra = TypeVar ("_ToT_contra" , bound = Shape , default = Any , contravariant = True )
88
56
_RankT_co = TypeVar ("_RankT_co" , bound = Shape , default = Any , covariant = True )
@@ -98,8 +66,8 @@ class _CanBroadcastTo(Protocol[_ToT_contra, _RankT_co]):
98
66
def __broadcast_to__ (self , to : _ToT_contra , / ) -> _RankT_co : ...
99
67
100
68
# 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
103
71
# are related to "unions vs joins".
104
72
105
73
_OwnShapeT_contra = TypeVar ("_OwnShapeT_contra" , bound = Shape , default = Any , contravariant = True )
@@ -129,46 +97,46 @@ class _BaseRankM(
129
97
130
98
@final
131
99
@type_check_only
132
- class Rank0 (_BaseRankM [_Shape0To0 , Shape0ToN , Shape0 ], tuple [()]): ...
100
+ class Rank0 (_BaseRankM [_Shape00 , Shape0N , Shape0 ], tuple [()]): ...
133
101
134
102
@final
135
103
@type_check_only
136
- class Rank1 (_BaseRankM [_Shape0To1 , Shape1ToN , Shape1 ], tuple [int ]): ...
104
+ class Rank1 (_BaseRankM [_Shape01 , Shape1N , Shape1 ], tuple [int ]): ...
137
105
138
106
@final
139
107
@type_check_only
140
- class Rank2 (_BaseRankM [_Shape0To2 , Shape2ToN , Shape2 ], tuple [int , int ]): ...
108
+ class Rank2 (_BaseRankM [_Shape02 , Shape2N , Shape2 ], tuple [int , int ]): ...
141
109
142
110
@final
143
111
@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 ]): ...
145
113
146
114
@final
147
115
@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 ]): ...
149
117
150
118
# this emulates `AnyOf`, rather than a `Union`.
151
119
@type_check_only
152
- class _BaseRankMToN (_BaseRank [Shape0ToN , _OwnShapeT , _OwnShapeT ], Generic [_OwnShapeT ]): ...
120
+ class _BaseRankMToN (_BaseRank [Shape0N , _OwnShapeT , _OwnShapeT ], Generic [_OwnShapeT ]): ...
153
121
154
122
@final
155
123
@type_check_only
156
- class Rank (_BaseRankMToN [Shape0ToN ], tuple [int , ...]): ...
124
+ class Rank (_BaseRankMToN [Shape0N ], tuple [int , ...]): ...
157
125
158
126
@final
159
127
@type_check_only
160
- class Rank1N (_BaseRankMToN [Shape1ToN ], tuple [int , * tuple [int , ...]]): ...
128
+ class Rank1N (_BaseRankMToN [Shape1N ], tuple [int , * tuple [int , ...]]): ...
161
129
162
130
@final
163
131
@type_check_only
164
- class Rank2N (_BaseRankMToN [Shape2ToN ], tuple [int , int , * tuple [int , ...]]): ...
132
+ class Rank2N (_BaseRankMToN [Shape2N ], tuple [int , int , * tuple [int , ...]]): ...
165
133
166
134
@final
167
135
@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 , ...]]): ...
169
137
170
138
@final
171
139
@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 , ...]]): ...
173
141
174
142
Rank0N : TypeAlias = Rank
0 commit comments