Skip to content

Commit 02ec61b

Browse files
committed
refactor: unfinalize HasArrayNamespace
1 parent 20d56b1 commit 02ec61b

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/array_api_typing/_namespace.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
__all__ = ("HasArrayNamespace",)
22

33
from types import ModuleType
4-
from typing import Protocol, final
4+
from typing import Protocol
55
from typing_extensions import TypeVar
66

7-
T = TypeVar("T", bound=object, default=ModuleType) # PEP 696 default
7+
T_co = TypeVar("T_co", covariant=True, bound=object, default=ModuleType)
88

99

10-
@final
11-
class HasArrayNamespace(Protocol[T]): # type: ignore[misc] # see python/mypy#17288
10+
class HasArrayNamespace(Protocol[T_co]):
1211
"""Protocol for classes that have an `__array_namespace__` method.
1312
1413
Example:
@@ -26,4 +25,4 @@ class HasArrayNamespace(Protocol[T]): # type: ignore[misc] # see python/mypy#1
2625
2726
"""
2827

29-
def __array_namespace__(self, /, *, api_version: str | None = None) -> T: ... # noqa: PLW3201
28+
def __array_namespace__(self, /, *, api_version: str | None = None) -> T_co: ... # noqa: PLW3201

tests/test_namespace.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66

77
@runtime_checkable
8-
class CheckableHasArrayNamespace(xpt.HasArrayNamespace, Protocol): # type: ignore[misc]
9-
"""Runtime checkable version of HasArrayNamespace."""
8+
class CheckableHasArrayNamespace(xpt.HasArrayNamespace, Protocol):
9+
"""A runtime-checkable version of the HasArrayNamespace protocol."""
10+
11+
# This class is used to ensure that the protocol can be checked at runtime.
12+
# It inherits from xpt.HasArrayNamespace and is marked as runtime_checkable.
1013

1114

1215
class GoodArray:

0 commit comments

Comments
 (0)