Skip to content

Commit 56856f7

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

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-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: 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
6+
7+
8+
class HasNamespace(Protocol):
9+
"""Protocol for classes that have an `__array_namespace__` method.
10+
11+
This is for type-annotating objects that should
12+
13+
Example:
14+
>>> import array_api_typing as xpt
15+
>>>
16+
>>> class MyArray:
17+
... def __array_namespace__(self):
18+
... return object()
19+
>>>
20+
>>> x = MyArray()
21+
>>> def has_namespace(x: xpt.HasNamespace) -> bool:
22+
... return hasattr(x, "__array_namespace__")
23+
>>> has_namespace(x)
24+
True
25+
26+
"""
27+
28+
def __array_namespace__(self) -> object: ... # noqa: PLW3201

0 commit comments

Comments
 (0)