Skip to content

Commit fe4a20c

Browse files
committed
✨: make HasNamespace
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
1 parent 4b6c44c commit fe4a20c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/array_api_typing/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Static typing support for the array API standard."""
22

3-
__all__ = ()
3+
__all__ = ["HasNamespace"]
44

55
from ._version import version as __version__ , version_tuple as __version_tuple__
6+
from ._namespace import HasNamespace

src/array_api_typing/_namespace.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Static typing support for the array API standard."""
2+
3+
__all__ = ["HasNamespace"]
4+
5+
from typing import Protocol, runtime_checkable
6+
7+
@runtime_checkable
8+
class HasNamespace(Protocol):
9+
"""Protocol for classes that have an `__array_namespace__` method.
10+
11+
This is a runtime checkable protocol.
12+
13+
Example:
14+
15+
>>> import array_api_typing as apt
16+
>>>
17+
>>> class MyArray:
18+
... def __array_namespace__(self):
19+
... return "my namespace"
20+
>>>
21+
>>> x = MyArray()
22+
>>> print(isinstance(x, apt.HasNamespace))
23+
True
24+
25+
"""
26+
27+
def __array_namespace__(self) -> object:
28+
...

0 commit comments

Comments
 (0)