Skip to content

Commit 66b55f9

Browse files
committed
✨: make HasNamespace
Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent 4b6c44c commit 66b55f9

File tree

5 files changed

+49
-127
lines changed

5 files changed

+49
-127
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

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"Topic :: Scientific/Engineering",
2626
"Typing :: Typed",
2727
]
28-
dependencies = []
28+
dependencies = [
29+
"typing-extensions>=4.14.0",
30+
]
2931

3032
[project.urls]
3133
Repository = "https://github.com/data-apis/array-api-typing"

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Static typing support for the array API standard."""
2+
3+
__all__ = ["HasNamespace"]
4+
5+
from types import ModuleType
6+
from typing import Protocol, final
7+
from typing_extensions import TypeVar
8+
9+
T = TypeVar("T", bound=object, default=ModuleType) # PEP 696 default
10+
11+
12+
@final
13+
class HasNamespace(Protocol[T]): # type: ignore[misc] # see python/mypy#17288
14+
"""Protocol for classes that have an `__array_namespace__` method.
15+
16+
This is for type-annotating objects that should
17+
18+
Example:
19+
>>> import array_api_typing as xpt
20+
>>>
21+
>>> class MyArray:
22+
... def __array_namespace__(self):
23+
... return object()
24+
>>>
25+
>>> x = MyArray()
26+
>>> def has_namespace(x: xpt.HasNamespace) -> bool:
27+
... return hasattr(x, "__array_namespace__")
28+
>>> has_namespace(x)
29+
True
30+
31+
"""
32+
33+
def __array_namespace__(self, /, *, api_version: str | None = None) -> T: ... # noqa: PLW3201

0 commit comments

Comments
 (0)