Skip to content

MAINT: array_api_compat tweaks #285

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
Apr 25, 2025
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
3 changes: 3 additions & 0 deletions src/array_api_extra/_lib/_utils/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
is_torch_namespace,
is_writeable_array,
size,
to_device,
)
except ImportError:
from array_api_compat import (
Expand All @@ -45,6 +46,7 @@
is_torch_namespace,
is_writeable_array,
size,
to_device,
)

__all__ = [
Expand All @@ -67,4 +69,5 @@
"is_torch_namespace",
"is_writeable_array",
"size",
"to_device",
]
43 changes: 24 additions & 19 deletions src/array_api_extra/_lib/_utils/_compat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

from types import ModuleType
from typing import Any, TypeGuard

# TODO import from typing (requires Python >=3.13)
from typing_extensions import TypeIs
Expand All @@ -12,29 +13,33 @@ from ._typing import Array, Device

# pylint: disable=missing-class-docstring,unused-argument

class Namespace(ModuleType):
def device(self, x: Array, /) -> Device: ...
Copy link
Contributor Author

@crusaderky crusaderky Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was plain wrong. This is literally the one function that is NOT in a Array API namespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha, good catch


def array_namespace(
*xs: Array | complex | None,
api_version: str | None = None,
use_compat: bool | None = None,
) -> Namespace: ...
) -> ModuleType: ...
def device(x: Array, /) -> Device: ...
def is_array_api_obj(x: object, /) -> TypeIs[Array]: ...
def is_array_api_strict_namespace(xp: ModuleType, /) -> TypeIs[Namespace]: ...
def is_cupy_namespace(xp: ModuleType, /) -> TypeIs[Namespace]: ...
def is_dask_namespace(xp: ModuleType, /) -> TypeIs[Namespace]: ...
def is_jax_namespace(xp: ModuleType, /) -> TypeIs[Namespace]: ...
def is_numpy_namespace(xp: ModuleType, /) -> TypeIs[Namespace]: ...
def is_pydata_sparse_namespace(xp: ModuleType, /) -> TypeIs[Namespace]: ...
def is_torch_namespace(xp: ModuleType, /) -> TypeIs[Namespace]: ...
def is_cupy_array(x: object, /) -> TypeIs[Array]: ...
def is_dask_array(x: object, /) -> TypeIs[Array]: ...
def is_jax_array(x: object, /) -> TypeIs[Array]: ...
def is_numpy_array(x: object, /) -> TypeIs[Array]: ...
def is_pydata_sparse_array(x: object, /) -> TypeIs[Array]: ...
def is_torch_array(x: object, /) -> TypeIs[Array]: ...
def is_lazy_array(x: object, /) -> TypeIs[Array]: ...
def is_writeable_array(x: object, /) -> TypeIs[Array]: ...
def is_array_api_strict_namespace(xp: ModuleType, /) -> bool: ...
def is_cupy_namespace(xp: ModuleType, /) -> bool: ...
def is_dask_namespace(xp: ModuleType, /) -> bool: ...
def is_jax_namespace(xp: ModuleType, /) -> bool: ...
def is_numpy_namespace(xp: ModuleType, /) -> bool: ...
def is_pydata_sparse_namespace(xp: ModuleType, /) -> bool: ...
def is_torch_namespace(xp: ModuleType, /) -> bool: ...
def is_cupy_array(x: object, /) -> TypeGuard[Array]: ...
def is_dask_array(x: object, /) -> TypeGuard[Array]: ...
def is_jax_array(x: object, /) -> TypeGuard[Array]: ...
def is_numpy_array(x: object, /) -> TypeGuard[Array]: ...
def is_pydata_sparse_array(x: object, /) -> TypeGuard[Array]: ...
def is_torch_array(x: object, /) -> TypeGuard[Array]: ...
def is_lazy_array(x: object, /) -> TypeGuard[Array]: ...
def is_writeable_array(x: object, /) -> TypeGuard[Array]: ...
def size(x: Array, /) -> int | None: ...
def to_device( # type: ignore[explicit-any]
x: Array,
device: Device, # pylint: disable=redefined-outer-name
/,
*,
stream: int | Any | None = None,
) -> Array: ...
3 changes: 2 additions & 1 deletion vendor_tests/test_vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ def test_vendor_compat():
is_torch_namespace,
is_writeable_array,
size,
to_device,
)

x = xp.asarray([1, 2, 3])
assert array_namespace(x) is xp
device(x)
to_device(x, device(x))
assert is_array_api_obj(x)
assert is_array_api_strict_namespace(xp)
assert not is_cupy_array(x)
Expand Down