|
49 | 49 | This should be considered experimental for now, and it may well change. |
50 | 50 |
|
51 | 51 | .. autoclass:: ArithArrayContainer |
52 | | -.. class:: ArrayContainerT |
53 | | -
|
54 | | - A type variable with a lower bound of :class:`ArrayContainer`. |
| 52 | +.. autoclass:: ArrayContainerT |
55 | 53 |
|
56 | 54 | .. autoexception:: NotAnArrayContainerError |
57 | 55 |
|
|
125 | 123 | from types import GenericAlias, UnionType |
126 | 124 | from typing import ( |
127 | 125 | TYPE_CHECKING, |
128 | | - ClassVar, |
129 | | - Protocol, |
130 | 126 | TypeAlias, |
131 | | - TypeVar, |
132 | 127 | get_origin, |
133 | 128 | ) |
134 | 129 |
|
135 | 130 | # For use in singledispatch type annotations, because sphinx can't figure out |
136 | 131 | # what 'np' is. |
137 | 132 | import numpy |
138 | 133 | import numpy as np |
139 | | -from typing_extensions import Self, TypeIs |
| 134 | +from typing_extensions import TypeIs |
140 | 135 |
|
141 | | -from pytools.obj_array import ObjectArrayND |
| 136 | +from pytools.obj_array import ObjectArrayND as ObjectArrayND |
142 | 137 |
|
143 | | -from arraycontext.context import ( |
| 138 | +from arraycontext.typing import ( |
| 139 | + ArrayContainer, |
| 140 | + ArrayContainerT, |
144 | 141 | ArrayOrArithContainer, |
145 | | - ArrayOrArithContainerOrScalar, |
| 142 | + ArrayOrArithContainerOrScalar as ArrayOrArithContainerOrScalar, |
146 | 143 | ArrayOrContainerOrScalar, |
147 | 144 | ) |
148 | 145 |
|
149 | 146 |
|
150 | 147 | if TYPE_CHECKING: |
151 | 148 | from pymbolic.geometric_algebra import CoeffT, MultiVector |
152 | 149 |
|
153 | | - from arraycontext.context import ArrayContext, ArrayOrScalar |
154 | | - |
155 | | - |
156 | | -# {{{ ArrayContainer |
157 | | - |
158 | | -class _UserDefinedArrayContainer(Protocol): |
159 | | - # This is used as a type annotation in dataclasses that are processed |
160 | | - # by dataclass_array_container, where it's used to recognize attributes |
161 | | - # that are container-typed. |
162 | | - |
163 | | - # This method prevents ArrayContainer from matching any object, while |
164 | | - # matching numpy object arrays and many array containers. |
165 | | - __array_ufunc__: ClassVar[None] |
166 | | - |
167 | | - |
168 | | -ArrayContainer: TypeAlias = ( |
169 | | - ObjectArrayND[ArrayOrContainerOrScalar] |
170 | | - | _UserDefinedArrayContainer |
171 | | - ) |
172 | | - |
173 | | - |
174 | | -class _UserDefinedArithArrayContainer(_UserDefinedArrayContainer, Protocol): |
175 | | - # This is loose and permissive, assuming that any array can be added |
176 | | - # to any container. The alternative would be to plaster type-ignores |
177 | | - # on all those uses. Achieving typing precision on what broadcasting is |
178 | | - # allowable seems like a huge endeavor and is likely not feasible without |
179 | | - # a mypy plugin. Maybe some day? -AK, November 2024 |
180 | | - |
181 | | - def __neg__(self) -> Self: ... |
182 | | - def __abs__(self) -> Self: ... |
183 | | - def __add__(self, other: ArrayOrScalar | Self) -> Self: ... |
184 | | - def __radd__(self, other: ArrayOrScalar | Self) -> Self: ... |
185 | | - def __sub__(self, other: ArrayOrScalar | Self) -> Self: ... |
186 | | - def __rsub__(self, other: ArrayOrScalar | Self) -> Self: ... |
187 | | - def __mul__(self, other: ArrayOrScalar | Self) -> Self: ... |
188 | | - def __rmul__(self, other: ArrayOrScalar | Self) -> Self: ... |
189 | | - def __truediv__(self, other: ArrayOrScalar | Self) -> Self: ... |
190 | | - def __rtruediv__(self, other: ArrayOrScalar | Self) -> Self: ... |
191 | | - def __pow__(self, other: ArrayOrScalar | Self) -> Self: ... |
192 | | - def __rpow__(self, other: ArrayOrScalar | Self) -> Self: ... |
193 | | - |
194 | | - |
195 | | -ArithArrayContainer: TypeAlias = ( |
196 | | - ObjectArrayND[ArrayOrArithContainerOrScalar] |
197 | | - | _UserDefinedArithArrayContainer) |
198 | | - |
| 150 | + from arraycontext.context import ArrayContext |
| 151 | + from arraycontext.typing import ArrayOrScalar as ArrayOrScalar |
199 | 152 |
|
200 | | -ArrayContainerT = TypeVar("ArrayContainerT", bound=ArrayContainer) |
201 | 153 |
|
| 154 | +# {{{ ArrayContainer traversals |
202 | 155 |
|
203 | 156 | class NotAnArrayContainerError(TypeError): |
204 | 157 | """:class:`TypeError` subclass raised when an array container is expected.""" |
|
0 commit comments