Skip to content
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

Array namespace #685

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/_array_api_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
('py:obj', "typing.Union[int, float, typing.Literal[inf, - inf]]"),
('py:class', 'enum.Enum'),
('py:class', 'ellipsis'),
("py:class", "ArrayAPINamespace"),
]
nitpick_ignore_regex = [
('py:class', '.*array'),
Expand Down
11 changes: 11 additions & 0 deletions src/array_api_stubs/_draft/_namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__all__ = ["ArrayAPINamespace"]

from typing import Protocol

from .creation_functions import arange as ArangeCallable


class ArrayAPINamespace(Protocol):
Copy link
Member

Choose a reason for hiding this comment

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

The camelcase works out slightly unfortunately here, but I think it's fine. ArrayApiNamespace or ArrayAPI_Namespace are more readable but not consistent to the standard rule.

Copy link
Contributor Author

@nstarman nstarman Sep 12, 2023

Choose a reason for hiding this comment

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

Agreed! Alternatively, we could drop the API bit in the middle?
Then this looks good: __array_namespace__() -> ArrayNamespace: ...

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, we could drop the API bit in the middle?

That sounds like the best idea here to me.

"""Protocol for the array API namespace itself."""

arange: ArangeCallable
7 changes: 6 additions & 1 deletion src/array_api_stubs/_draft/array_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from ._types import (
array,
dtype as Dtype,
Expand All @@ -13,6 +15,9 @@
ellipsis,
)

if TYPE_CHECKING:
from ._namespace import ArrayAPINamespace


class _array:
def __init__(self: array) -> None:
Expand Down Expand Up @@ -195,7 +200,7 @@ def __and__(self: array, other: Union[int, bool, array], /) -> array:

def __array_namespace__(
self: array, /, *, api_version: Optional[str] = None
) -> Any:
) -> ArrayAPINamespace:
"""
Returns an object that has all the array API functions on it.

Expand Down
23 changes: 14 additions & 9 deletions src/array_api_stubs/_draft/creation_functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Protocol
from ._types import (
List,
NestedSequence,
Expand All @@ -11,15 +12,7 @@
)


def arange(
start: Union[int, float],
/,
stop: Optional[Union[int, float]] = None,
step: Union[int, float] = 1,
*,
dtype: Optional[dtype] = None,
device: Optional[device] = None,
) -> array:
class arange(Protocol):
"""
Returns evenly spaced values within the half-open interval ``[start, stop)`` as a one-dimensional array.

Expand All @@ -46,6 +39,18 @@ def arange(
a one-dimensional array containing evenly spaced values. The length of the output array must be ``ceil((stop-start)/step)`` if ``stop - start`` and ``step`` have the same sign, and length ``0`` otherwise.
"""

def __call__(
self,
start: Union[int, float],
/,
stop: Optional[Union[int, float]] = None,
step: Union[int, float] = 1,
*,
dtype: Optional[dtype] = None,
device: Optional[device] = None,
) -> array:
...


def asarray(
obj: Union[
Expand Down