Skip to content

Commit 1561707

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

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ venv/
2222

2323
# Build docs
2424
/src/array_api_typing/_version.py
25+
26+
# Mac files
27+
.DS_Store

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

0 commit comments

Comments
 (0)