Skip to content

Commit c347669

Browse files
committed
implement a workaround for python/mypy#18184
1 parent 1b93c2e commit c347669

3 files changed

Lines changed: 39 additions & 25 deletions

File tree

‎optype/numpy/_sequence_nd.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Any, Protocol, TypeVar
2+
3+
4+
T_co = TypeVar("T_co", covariant=True)
5+
6+
7+
class SequenceND(Protocol[T_co]):
8+
# a slimmed down version of `numpy._typing._NestedSequence`
9+
def __len__(self, /) -> int: ...
10+
def __getitem__(self, index: int, /) -> "T_co | SequenceND[T_co]": ...
11+
def __contains__(self, x: object, /) -> bool: ...
12+
def index(self, value: Any, /) -> int: ... # type: ignore[no-any-explicit] # pyright: ignore[reportAny, reportExplicitAny]

‎optype/numpy/_to.py‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import sys
24
from collections.abc import Sequence
35
from typing import TypeAlias, TypeVar
@@ -6,6 +8,7 @@
68

79
from ._array import CanArrayND
810
from ._scalar import floating, integer, number
11+
from ._sequence_nd import SequenceND
912

1013

1114
if sys.version_info >= (3, 13):
@@ -40,33 +43,37 @@
4043
T = TypeVar("T")
4144
ST = TypeVar("ST", bound=np.generic)
4245

46+
4347
_To1D: TypeAlias = CanArrayND[ST] | Sequence[T | ST]
4448
_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]]
4552

4653
ToScalar: TypeAlias = complex | bytes | str | np.generic
4754
ToArray1D: TypeAlias = _To1D[np.generic, complex | bytes | str]
4855
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]
5057

5158
ToBool: TypeAlias = bool | np.bool_
5259
ToBool1D: TypeAlias = _To1D[np.bool_, bool]
5360
ToBool2D: TypeAlias = _To2D[np.bool_, bool]
54-
ToBoolND: TypeAlias = _To1D[np.bool_, bool] | Sequence["ToBoolND"]
61+
ToBoolND: TypeAlias = _ToND[np.bool_, bool]
5562

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]
6168

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]
6774

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]

‎tests/numpy/test_to.pyi‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import numpy as np
44
import optype.numpy as onp
55

66
vec_b: onp.Array1D[np.bool] | list[bool | np.bool_]
7-
vec_i: onp.Array1D[np.intp] | list[int | np.intp]
8-
vec_f: onp.Array1D[np.longdouble] | list[float | np.longdouble]
9-
vec_c: onp.Array1D[np.clongdouble] | list[complex | np.clongdouble]
7+
vec_i: onp.Array1D[np.intp] | list[int | np.intp] | range
8+
vec_f: onp.Array1D[np.longdouble] | list[float | np.longdouble] | range
9+
vec_c: onp.Array1D[np.clongdouble] | list[complex | np.clongdouble] | range
1010

1111
mat_b: onp.Array2D[np.bool] | list[list[bool | np.bool_]]
1212
mat_i: onp.Array2D[np.intp] | list[list[int | np.intp]]
@@ -90,11 +90,6 @@ tsi: onp.ToIntND = 42 # type: ignore[assignment] # pyright: ignore[reportAssig
9090
tsf: onp.ToFloatND = 42.0 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
9191
tsc: onp.ToComplexND = 42 + 0j # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
9292

93-
tob: onp.ToBoolND = object() # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
94-
toi: onp.ToIntND = object() # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
95-
tof: onp.ToFloatND = object() # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
96-
toc: onp.ToComplexND = object() # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
97-
9893
tub: onp.ToBoolND = "illegal" # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
9994
tui: onp.ToIntND = "illegal" # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
10095
tuf: onp.ToFloatND = "illegal" # type: ignore[assignment] # pyright: ignore[reportAssignmentType]

0 commit comments

Comments
 (0)