Skip to content

Commit 484c7dd

Browse files
authored
MAINT: hide some typing hacks away (#934)
1 parent cf5209a commit 484c7dd

5 files changed

Lines changed: 44 additions & 43 deletions

File tree

src/array_api_extra/_lib/_helpers.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import warnings
1212
from collections.abc import Callable, Generator, Iterable, Iterator, Sequence
1313
from typing import (
14-
TYPE_CHECKING,
1514
Any,
1615
ClassVar,
1716
Generic,
@@ -22,16 +21,7 @@
2221
)
2322

2423
from . import _compat
25-
from ._typing import Array, ArrayNamespace
26-
27-
if TYPE_CHECKING: # pragma: no cover
28-
# TODO import from typing (requires Python >=3.12 and >=3.13)
29-
from typing_extensions import TypeIs, override
30-
else:
31-
32-
def override(func):
33-
return func
34-
24+
from ._typing import Array, ArrayNamespace, TypeIs, override
3525

3626
P = ParamSpec("P")
3727
T = TypeVar("T")
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
1-
"""Static typing helpers."""
2-
# numpydoc ignore=GL08
1+
"""Static typing helpers — better implementations in the stub file."""
32

3+
from collections.abc import Callable
44
from types import ModuleType
5+
from typing import Any
56

67
Array = object
78
ArrayLike = object
89
ArrayNamespace = ModuleType
910
DType = object
1011
Device = object
12+
Key = object
1113
GetIndex = object
14+
Graph = object
1215
NumPyObject = object
16+
SchedulerGetCallable = object
1317
SetIndex = object
1418

19+
TypeIs = Any
20+
1521
__all__ = [
1622
"Array",
23+
"ArrayLike",
1724
"ArrayNamespace",
1825
"DType",
1926
"Device",
2027
"GetIndex",
28+
"Graph",
29+
"Key",
2130
"NumPyObject",
31+
"SchedulerGetCallable",
2232
"SetIndex",
33+
"TypeIs",
34+
"override",
2335
]
36+
37+
38+
def override(func: Callable[..., Any]) -> Callable[..., Any]: # numpydoc ignore=GL08
39+
return func

src/array_api_extra/_lib/_typing.pyi

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ from types import EllipsisType, ModuleType
44
from typing import Any, Protocol, TypeAlias
55

66
import numpy as np
7+
from dask.typing import Graph, Key, SchedulerGetCallable
78
from numpy.typing import ArrayLike
89

9-
# TODO import from typing (requires Python >=3.12)
10-
from typing_extensions import override
10+
# TODO import from typing (requires Python >=3.12 and >=3.13)
11+
from typing_extensions import TypeIs, override
1112

1213
# TODO: use array-api-typing once it is available
1314

@@ -112,6 +113,11 @@ __all__ = [
112113
"DType",
113114
"Device",
114115
"GetIndex",
116+
"Graph",
117+
"Key",
115118
"NumPyObject",
119+
"SchedulerGetCallable",
116120
"SetIndex",
121+
"TypeIs",
122+
"override",
117123
]

src/array_api_extra/testing/_testing.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@
1111
import warnings
1212
from collections.abc import Callable, Generator, Iterator, Sequence
1313
from types import FunctionType, ModuleType
14-
from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar
14+
from typing import Any, ParamSpec, TypeVar
1515

1616
from .._lib import _compat, _helpers
17-
from .._lib._typing import Array, ArrayNamespace, Device
17+
from .._lib._typing import (
18+
Array,
19+
ArrayNamespace,
20+
Device,
21+
Graph,
22+
Key,
23+
SchedulerGetCallable,
24+
override,
25+
)
26+
27+
if typing.TYPE_CHECKING:
28+
import numpy as np
29+
import pytest
1830

1931
__all__ = [
2032
"assert_close",
@@ -25,20 +37,6 @@
2537
"patch_lazy_xp_functions",
2638
]
2739

28-
if TYPE_CHECKING: # pragma: no cover
29-
# TODO import override from typing (requires Python >=3.12)
30-
import numpy as np
31-
import pytest
32-
from dask.typing import Graph, Key, SchedulerGetCallable
33-
from typing_extensions import override as _override
34-
35-
else:
36-
# Sphinx hacks
37-
SchedulerGetCallable = object
38-
39-
def _override(func):
40-
return func
41-
4240

4341
__all__ = [
4442
"assert_close",
@@ -493,7 +491,7 @@ def __init__(self, max_count: int, msg: str) -> None: # numpydoc ignore=GL08
493491
self.max_count = max_count
494492
self.msg = msg
495493

496-
@_override
494+
@override
497495
def __call__(
498496
self, dsk: Graph, keys: Sequence[Key] | Key, **kwargs: Any
499497
) -> Any: # numpydoc ignore=GL08

tests/test_helpers.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Iterator
2-
from typing import TYPE_CHECKING, Generic, TypeVar, cast
2+
from typing import Generic, TypeVar, cast
33

44
import numpy as np
55
import pytest
@@ -18,20 +18,11 @@
1818
pickle_flatten,
1919
pickle_unflatten,
2020
)
21-
from array_api_extra._lib._typing import Array, ArrayNamespace, Device, DType
21+
from array_api_extra._lib._typing import Array, ArrayNamespace, Device, DType, override
2222
from array_api_extra.testing import assert_equal, lazy_xp_function
2323

2424
from .conftest import np_compat
2525

26-
if TYPE_CHECKING: # pragma: no cover
27-
# TODO import from typing (requires Python >=3.12)
28-
from typing_extensions import override
29-
else:
30-
31-
def override(func):
32-
return func
33-
34-
3526
T = TypeVar("T")
3627

3728
# FIXME calls xp.unique_values without size

0 commit comments

Comments
 (0)