|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import sys |
2 | 4 | from collections.abc import Sequence |
3 | 5 | from typing import TypeAlias, TypeVar |
|
6 | 8 |
|
7 | 9 | from ._array import CanArrayND |
8 | 10 | from ._scalar import floating, integer, number |
| 11 | +from ._sequence_nd import SequenceND |
9 | 12 |
|
10 | 13 |
|
11 | 14 | if sys.version_info >= (3, 13): |
|
40 | 43 | T = TypeVar("T") |
41 | 44 | ST = TypeVar("ST", bound=np.generic) |
42 | 45 |
|
| 46 | + |
43 | 47 | _To1D: TypeAlias = CanArrayND[ST] | Sequence[T | ST] |
44 | 48 | _To2D: TypeAlias = CanArrayND[ST] | Sequence[_To1D[ST, T]] |
| 49 | +# recursive sequence type cannot be used due to a mypy bug: |
| 50 | +# https://github.com/python/mypy/issues/18184 |
| 51 | +_ToND: TypeAlias = CanArrayND[ST] | SequenceND[T | ST] | SequenceND[CanArrayND[ST]] |
45 | 52 |
|
46 | 53 | ToScalar: TypeAlias = complex | bytes | str | np.generic |
47 | 54 | ToArray1D: TypeAlias = _To1D[np.generic, complex | bytes | str] |
48 | 55 | ToArray2D: TypeAlias = _To2D[np.generic, complex | bytes | str] |
49 | | -ToArrayND: TypeAlias = _To1D[np.generic, complex | bytes | str] | Sequence["ToArrayND"] |
| 56 | +ToArrayND: TypeAlias = _ToND[np.generic, complex | bytes | str] |
50 | 57 |
|
51 | 58 | ToBool: TypeAlias = bool | np.bool_ |
52 | 59 | ToBool1D: TypeAlias = _To1D[np.bool_, bool] |
53 | 60 | ToBool2D: TypeAlias = _To2D[np.bool_, bool] |
54 | | -ToBoolND: TypeAlias = _To1D[np.bool_, bool] | Sequence["ToBoolND"] |
| 61 | +ToBoolND: TypeAlias = _ToND[np.bool_, bool] |
55 | 62 |
|
56 | | -_i_co = TypeAliasType("_i_co", integer | np.bool_) # type: ignore[no-any-explicit] |
57 | | -ToInt: TypeAlias = int | _i_co |
58 | | -ToInt1D: TypeAlias = _To1D[_i_co, int] |
59 | | -ToInt2D: TypeAlias = _To2D[_i_co, int] |
60 | | -ToIntND: TypeAlias = _To1D[_i_co, int] | Sequence["ToIntND"] |
| 63 | +integer_co = TypeAliasType("integer_co", integer | np.bool_) # type: ignore[no-any-explicit] |
| 64 | +ToInt: TypeAlias = int | integer_co |
| 65 | +ToInt1D: TypeAlias = _To1D[integer_co, int] |
| 66 | +ToInt2D: TypeAlias = _To2D[integer_co, int] |
| 67 | +ToIntND: TypeAlias = _ToND[integer_co, int] |
61 | 68 |
|
62 | | -_f_co = TypeAliasType("_f_co", floating | integer | np.bool_) # type: ignore[no-any-explicit] |
63 | | -ToFloat: TypeAlias = float | _f_co |
64 | | -ToFloat1D: TypeAlias = _To1D[_f_co, float] |
65 | | -ToFloat2D: TypeAlias = _To2D[_f_co, float] |
66 | | -ToFloatND: TypeAlias = _To1D[_f_co, float] | Sequence["ToFloatND"] |
| 69 | +floating_co = TypeAliasType("floating_co", floating | integer | np.bool_) # type: ignore[no-any-explicit] |
| 70 | +ToFloat: TypeAlias = float | floating_co |
| 71 | +ToFloat1D: TypeAlias = _To1D[floating_co, float] |
| 72 | +ToFloat2D: TypeAlias = _To2D[floating_co, float] |
| 73 | +ToFloatND: TypeAlias = _ToND[floating_co, float] |
67 | 74 |
|
68 | | -_c_co = TypeAliasType("_c_co", number | np.bool_) # type: ignore[no-any-explicit] |
69 | | -ToComplex: TypeAlias = complex | _c_co |
70 | | -ToComplex1D: TypeAlias = _To1D[_c_co, complex] |
71 | | -ToComplex2D: TypeAlias = _To2D[_c_co, complex] |
72 | | -ToComplexND: TypeAlias = _To1D[_c_co, complex] | Sequence["ToComplexND"] |
| 75 | +complexfloating_co = TypeAliasType("complexfloating_co", number | np.bool_) # type: ignore[no-any-explicit] |
| 76 | +ToComplex: TypeAlias = complex | complexfloating_co |
| 77 | +ToComplex1D: TypeAlias = _To1D[complexfloating_co, complex] |
| 78 | +ToComplex2D: TypeAlias = _To2D[complexfloating_co, complex] |
| 79 | +ToComplexND: TypeAlias = _ToND[complexfloating_co, complex] |
0 commit comments