Skip to content

✨: create HasArrayNamepace #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ venv/

# Build docs
/src/array_api_typing/_version.py

# Mac files
.DS_Store
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<h1 align="center"> array-api-typing </h1>
<h3 align="center">Type Annotations for the Python array API standard</h3>

---

## Installation

```bash
pip install array-api-typing
```

<details>
<summary>using <code>uv</code></summary>

```bash
uv add array-api-typing
```

</details>
<details>
<summary>from source, using pip</summary>

```bash
pip install git+https://github.com/data-apis/array-api-typing.git
```

</details>
<details>
<summary>building from source</summary>

```bash
cd /path/to/parent
git clone https://github.com/data-apis/array-api-typing.git
cd array-api-typing
pip install -e . # editable mode
```

</details>

### Quick example

```pycon
>>> import array_api_typing as xpt
>>> import numpy as np

>>> def func(x: xpt.HasArrayNamespace) -> xpt.HasArrayNamespace:
... return x

>>> func(np.array([1, 2, 3]))
array([1, 2, 3])

```
16 changes: 16 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Pytest configuration file."""

from sybil import Sybil
from sybil.parsers.doctest import DocTestParser

readme_tester = Sybil(
parsers=[DocTestParser()],
pattern="README.md",
)

python_file_tester = Sybil(
parsers=[DocTestParser()],
pattern="src/**/*.py",
)

pytest_collect_file = (readme_tester + python_file_tester).pytest()
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"Topic :: Scientific/Engineering",
"Typing :: Typed",
]
dependencies = []
dependencies = [
"typing-extensions>=4.14.0",
]

[project.urls]
Repository = "https://github.com/data-apis/array-api-typing"
Expand All @@ -43,6 +45,7 @@
"lefthook>=1.11.13",
]
test = [
"numpy>=1.24",
"pytest>=8.3.3",
"pytest-cov>=3",
"pytest-github-actions-annotate-failures>=0.3.0",
Expand Down Expand Up @@ -83,6 +86,10 @@ version_tuple = {version_tuple!r}
warn_unreachable = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = "sybil.*"
ignore_missing_imports = true


[tool.pytest.ini_options]
addopts = [
Expand Down
5 changes: 3 additions & 2 deletions src/array_api_typing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Static typing support for the array API standard."""

__all__ = ()
__all__ = ["HasArrayNamespace", "__version__", "__version_tuple__"]

from ._version import version as __version__ , version_tuple as __version_tuple__
from ._namespace import HasArrayNamespace
from ._version import version as __version__, version_tuple as __version_tuple__
31 changes: 31 additions & 0 deletions src/array_api_typing/_namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Static typing support for the array API standard."""

__all__ = ["HasArrayNamespace"]

from types import ModuleType
from typing import Protocol, final
from typing_extensions import TypeVar

T = TypeVar("T", bound=object, default=ModuleType) # PEP 696 default


@final
class HasArrayNamespace(Protocol[T]): # type: ignore[misc] # see python/mypy#17288
"""Protocol for classes that have an `__array_namespace__` method.

Example:
>>> import array_api_typing as xpt
>>>
>>> class MyArray:
... def __array_namespace__(self):
... return object()
>>>
>>> x = MyArray()
>>> def has_array_namespace(x: xpt.HasArrayNamespace) -> bool:
... return hasattr(x, "__array_namespace__")
>>> has_array_namespace(x)
True

"""

def __array_namespace__(self, /, *, api_version: str | None = None) -> T: ... # noqa: PLW3201
257 changes: 138 additions & 119 deletions uv.lock

Large diffs are not rendered by default.