Skip to content

Update api_version argument to __array_namespace__ #24

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

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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
6 changes: 5 additions & 1 deletion array_api_strict/_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import operator
from enum import IntEnum
import warnings

from ._creation_functions import asarray
from ._dtypes import (
_DType,
Expand Down Expand Up @@ -480,8 +482,10 @@ def __and__(self: Array, other: Union[int, bool, Array], /) -> Array:
def __array_namespace__(
self: Array, /, *, api_version: Optional[str] = None
) -> types.ModuleType:
if api_version is not None and not api_version.startswith("2021."):
if api_version is not None and api_version not in ["2021.12", "2022.12"]:
raise ValueError(f"Unrecognized array API version: {api_version!r}")
if api_version == "2021.12":
warnings.warn("The 2021.12 version of the array API specification was requested but the returned namespace is actually version 2022.12")
import array_api_strict
return array_api_strict

Expand Down
12 changes: 11 additions & 1 deletion array_api_strict/tests/test_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
uint64,
bool as bool_,
)

import array_api_strict

def test_validate_index():
# The indexing tests in the official array API test suite test that the
Expand Down Expand Up @@ -398,3 +398,13 @@ def test_array_keys_use_private_array():
key = ones((0, 0), dtype=bool_)
with pytest.raises(IndexError):
a[key]

def test_array_namespace():
a = ones((3, 3))
assert a.__array_namespace__() == array_api_strict
assert a.__array_namespace__(api_version=None) is array_api_strict
assert a.__array_namespace__(api_version="2022.12") is array_api_strict
with pytest.warns(UserWarning):
assert a.__array_namespace__(api_version="2021.12") is array_api_strict
pytest.raises(ValueError, lambda: a.__array_namespace__(api_version="2021.11"))
pytest.raises(ValueError, lambda: a.__array_namespace__(api_version="2023.12"))