Skip to content

Commit 1d187c1

Browse files
nstarmanjorenhamlucascolley
authored
✨: create HasArrayNamepace (#17)
Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com> Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>
1 parent 4b6c44c commit 1d187c1

File tree

7 files changed

+252
-123
lines changed

7 files changed

+252
-123
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

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<h1 align="center"> array-api-typing </h1>
2+
<h3 align="center">Type Annotations for the Python array API standard</h3>
3+
4+
---
5+
6+
## Installation
7+
8+
```bash
9+
pip install array-api-typing
10+
```
11+
12+
<details>
13+
<summary>using <code>uv</code></summary>
14+
15+
```bash
16+
uv add array-api-typing
17+
```
18+
19+
</details>
20+
<details>
21+
<summary>from source, using pip</summary>
22+
23+
```bash
24+
pip install git+https://github.com/data-apis/array-api-typing.git
25+
```
26+
27+
</details>
28+
<details>
29+
<summary>building from source</summary>
30+
31+
```bash
32+
cd /path/to/parent
33+
git clone https://github.com/data-apis/array-api-typing.git
34+
cd array-api-typing
35+
pip install -e . # editable mode
36+
```
37+
38+
</details>
39+
40+
### Quick example
41+
42+
```pycon
43+
>>> import array_api_typing as xpt
44+
>>> import numpy as np
45+
46+
>>> def func(x: xpt.HasArrayNamespace) -> xpt.HasArrayNamespace:
47+
... return x
48+
49+
>>> func(np.array([1, 2, 3]))
50+
array([1, 2, 3])
51+
52+
```

conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Pytest configuration file."""
2+
3+
from sybil import Sybil
4+
from sybil.parsers.doctest import DocTestParser
5+
6+
readme_tester = Sybil(
7+
parsers=[DocTestParser()],
8+
pattern="README.md",
9+
)
10+
11+
python_file_tester = Sybil(
12+
parsers=[DocTestParser()],
13+
pattern="src/**/*.py",
14+
)
15+
16+
pytest_collect_file = (readme_tester + python_file_tester).pytest()

pyproject.toml

Lines changed: 9 additions & 2 deletions
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"
@@ -41,8 +43,9 @@
4143
dev = [
4244
{ include-group = "test" },
4345
"lefthook>=1.11.13",
44-
]
46+
]
4547
test = [
48+
"numpy>=1.25",
4649
"pytest>=8.3.3",
4750
"pytest-cov>=3",
4851
"pytest-github-actions-annotate-failures>=0.3.0",
@@ -83,6 +86,10 @@ version_tuple = {version_tuple!r}
8386
warn_unreachable = true
8487
warn_unused_configs = true
8588

89+
[[tool.mypy.overrides]]
90+
module = "sybil.*"
91+
ignore_missing_imports = true
92+
8693

8794
[tool.pytest.ini_options]
8895
addopts = [

src/array_api_typing/__init__.py

Lines changed: 3 additions & 2 deletions
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__ = ["HasArrayNamespace", "__version__", "__version_tuple__"]
44

5-
from ._version import version as __version__ , version_tuple as __version_tuple__
5+
from ._namespace import HasArrayNamespace
6+
from ._version import version as __version__, version_tuple as __version_tuple__

src/array_api_typing/_namespace.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Static typing support for the array API standard."""
2+
3+
__all__ = ["HasArrayNamespace"]
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 HasArrayNamespace(Protocol[T]): # type: ignore[misc] # see python/mypy#17288
14+
"""Protocol for classes that have an `__array_namespace__` method.
15+
16+
Example:
17+
>>> import array_api_typing as xpt
18+
>>>
19+
>>> class MyArray:
20+
... def __array_namespace__(self):
21+
... return object()
22+
>>>
23+
>>> x = MyArray()
24+
>>> def has_array_namespace(x: xpt.HasArrayNamespace) -> bool:
25+
... return hasattr(x, "__array_namespace__")
26+
>>> has_array_namespace(x)
27+
True
28+
29+
"""
30+
31+
def __array_namespace__(self, /, *, api_version: str | None = None) -> T: ... # noqa: PLW3201

0 commit comments

Comments
 (0)