From 6e57d829cb6628610e163524f203245b247a2839 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Wed, 4 Aug 2021 16:47:05 -0600 Subject: Rename numpy._array_api to numpy.array_api Instead of the leading underscore, the experimentalness of the module will be indicated by omitting a warning on import. That we, we do not have to change the API from underscore to no underscore when the module is no longer experimental. --- numpy/array_api/_typing.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 numpy/array_api/_typing.py (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py new file mode 100644 index 000000000..4ff718205 --- /dev/null +++ b/numpy/array_api/_typing.py @@ -0,0 +1,26 @@ +""" +This file defines the types for type annotations. + +These names aren't part of the module namespace, but they are used in the +annotations in the function signatures. The functions in the module are only +valid for inputs that match the given type annotations. +""" + +__all__ = ['Array', 'Device', 'Dtype', 'SupportsDLPack', + 'SupportsBufferProtocol', 'PyCapsule'] + +from typing import Any, Sequence, Type, Union + +from . import (Array, int8, int16, int32, int64, uint8, uint16, uint32, + uint64, float32, float64) + +# This should really be recursive, but that isn't supported yet. See the +# similar comment in numpy/typing/_array_like.py +NestedSequence = Sequence[Sequence[Any]] + +Device = Any +Dtype = Type[Union[[int8, int16, int32, int64, uint8, uint16, + uint32, uint64, float32, float64]]] +SupportsDLPack = Any +SupportsBufferProtocol = Any +PyCapsule = Any -- cgit v1.2.1 From 8f7d00ed447174d9398af3365709222b529c1cad Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Fri, 6 Aug 2021 18:22:00 -0600 Subject: Run (selective) black on the array_api submodule I've omitted a few changes from black that messed up the readability of some complicated if statements that were organized logically line-by-line, and some changes that use unnecessary operator spacing. --- numpy/array_api/_typing.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 4ff718205..d530a91ae 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -6,21 +6,39 @@ annotations in the function signatures. The functions in the module are only valid for inputs that match the given type annotations. """ -__all__ = ['Array', 'Device', 'Dtype', 'SupportsDLPack', - 'SupportsBufferProtocol', 'PyCapsule'] +__all__ = [ + "Array", + "Device", + "Dtype", + "SupportsDLPack", + "SupportsBufferProtocol", + "PyCapsule", +] from typing import Any, Sequence, Type, Union -from . import (Array, int8, int16, int32, int64, uint8, uint16, uint32, - uint64, float32, float64) +from . import ( + Array, + int8, + int16, + int32, + int64, + uint8, + uint16, + uint32, + uint64, + float32, + float64, +) # This should really be recursive, but that isn't supported yet. See the # similar comment in numpy/typing/_array_like.py NestedSequence = Sequence[Sequence[Any]] Device = Any -Dtype = Type[Union[[int8, int16, int32, int64, uint8, uint16, - uint32, uint64, float32, float64]]] +Dtype = Type[ + Union[[int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64]] +] SupportsDLPack = Any SupportsBufferProtocol = Any PyCapsule = Any -- cgit v1.2.1 From e4a11cb29af12b0e67fda3f924585b23cee0b567 Mon Sep 17 00:00:00 2001 From: Sista Seetaram Date: Fri, 3 Sep 2021 00:25:57 +0530 Subject: fixed unhashable instance and potential exception as listed in LGTM#19077 --- numpy/array_api/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index d530a91ae..8b7832d74 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -37,7 +37,7 @@ NestedSequence = Sequence[Sequence[Any]] Device = Any Dtype = Type[ - Union[[int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64]] + Union[(int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64)] ] SupportsDLPack = Any SupportsBufferProtocol = Any -- cgit v1.2.1 From e4c589054b342dc750cdd09686c0c01ab762679c Mon Sep 17 00:00:00 2001 From: Sista Seetaram Date: Fri, 3 Sep 2021 06:09:38 +0530 Subject: fix unhashable instance and potential exception identified by LGTM --- numpy/array_api/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 8b7832d74..831a108bc 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -37,7 +37,7 @@ NestedSequence = Sequence[Sequence[Any]] Device = Any Dtype = Type[ - Union[(int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64)] + Union[int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64] ] SupportsDLPack = Any SupportsBufferProtocol = Any -- cgit v1.2.1 From 2d112a98ed7597c4120b31908384ae09b0304659 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Sat, 25 Sep 2021 17:34:22 -0500 Subject: ENH: Updates to numpy.array_api (#19937) * Add __index__ to array_api and update __int__, __bool__, and __float__ The spec specifies that they should only work on arrays with corresponding dtypes. __index__ is new in the spec since the initial PR, and works identically to np.array.__index__. * Add the to_device method to the array_api This method is new since #18585. It does nothing in NumPy since NumPy does not support non-CPU devices. * Update transpose methods in the array_api transpose() was renamed to matrix_transpose() and now operates on stacks of matrices. A function to permute dimensions will be added once it is finalized in the spec. The attribute mT was added and the T attribute was updated to only operate on 2-dimensional arrays as per the spec. * Restrict input dtypes in the array API statistical functions * Add the dtype parameter to the array API sum() and prod() * Add the function permute_dims() to the array_api namespace permute_dims() is the replacement for transpose(), which was split into permute_dims() and matrix_transpose(). * Add tril and triu to the array API namespace * Fix the array_api Array.__repr__ to indent the array properly * Make the Device type in the array_api just accept the string "cpu" --- numpy/array_api/_typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 831a108bc..5f937a56c 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -15,7 +15,7 @@ __all__ = [ "PyCapsule", ] -from typing import Any, Sequence, Type, Union +from typing import Any, Literal, Sequence, Type, Union from . import ( Array, @@ -35,7 +35,7 @@ from . import ( # similar comment in numpy/typing/_array_like.py NestedSequence = Sequence[Sequence[Any]] -Device = Any +Device = Literal["cpu"] Dtype = Type[ Union[int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64] ] -- cgit v1.2.1 From a5beccfa3574f4fcb1b6030737b728e65803791f Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 27 Sep 2021 14:43:16 +0200 Subject: MAINT: Fix invalid parameter types used in `Dtype` --- numpy/array_api/_typing.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 5f937a56c..f66279fbc 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -15,10 +15,12 @@ __all__ = [ "PyCapsule", ] -from typing import Any, Literal, Sequence, Type, Union +import sys +from typing import Any, Literal, Sequence, Type, Union, TYPE_CHECKING -from . import ( - Array, +from . import Array +from numpy import ( + dtype, int8, int16, int32, @@ -36,9 +38,22 @@ from . import ( NestedSequence = Sequence[Sequence[Any]] Device = Literal["cpu"] -Dtype = Type[ - Union[int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64] -] +if TYPE_CHECKING or sys.version_info >= (3, 9): + Dtype = dtype[Union[ + int8, + int16, + int32, + int64, + uint8, + uint16, + uint32, + uint64, + float32, + float64, + ]] +else: + Dtype = dtype + SupportsDLPack = Any SupportsBufferProtocol = Any PyCapsule = Any -- cgit v1.2.1 From 4f7e991960c24fc9548f8f3d6d5f8967c2ece84a Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 27 Sep 2021 14:44:02 +0200 Subject: MAINT: Add a missing subscription slot to `NestedSequence` --- numpy/array_api/_typing.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index f66279fbc..4785f5fe3 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -16,7 +16,7 @@ __all__ = [ ] import sys -from typing import Any, Literal, Sequence, Type, Union, TYPE_CHECKING +from typing import Any, Literal, Sequence, Type, Union, TYPE_CHECKING, TypeVar from . import Array from numpy import ( @@ -35,7 +35,8 @@ from numpy import ( # This should really be recursive, but that isn't supported yet. See the # similar comment in numpy/typing/_array_like.py -NestedSequence = Sequence[Sequence[Any]] +_T = TypeVar("_T") +NestedSequence = Sequence[Sequence[_T]] Device = Literal["cpu"] if TYPE_CHECKING or sys.version_info >= (3, 9): -- cgit v1.2.1 From 4f8f50d5b5e992afaf0ef08773bd88d696683bd3 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 27 Sep 2021 14:47:22 +0200 Subject: MAINT: Import `Array` from the `_array_object` namespace Changed as `Array` does not live in the main `np.array_api` namespace --- numpy/array_api/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 4785f5fe3..519e8463c 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -18,7 +18,7 @@ __all__ = [ import sys from typing import Any, Literal, Sequence, Type, Union, TYPE_CHECKING, TypeVar -from . import Array +from ._array_object import Array from numpy import ( dtype, int8, -- cgit v1.2.1 From f931a434839222bb00282a432d6d6a0c2c52eb7d Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 18 Oct 2021 18:10:47 +0200 Subject: ENH: Replace `NestedSequence` with a proper nested sequence protocol --- numpy/array_api/_typing.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 519e8463c..5e980b16f 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -6,6 +6,8 @@ annotations in the function signatures. The functions in the module are only valid for inputs that match the given type annotations. """ +from __future__ import annotations + __all__ = [ "Array", "Device", @@ -16,7 +18,16 @@ __all__ = [ ] import sys -from typing import Any, Literal, Sequence, Type, Union, TYPE_CHECKING, TypeVar +from typing import ( + Any, + Literal, + Sequence, + Type, + Union, + TYPE_CHECKING, + TypeVar, + Protocol, +) from ._array_object import Array from numpy import ( @@ -33,10 +44,11 @@ from numpy import ( float64, ) -# This should really be recursive, but that isn't supported yet. See the -# similar comment in numpy/typing/_array_like.py -_T = TypeVar("_T") -NestedSequence = Sequence[Sequence[_T]] +_T_co = TypeVar("_T_co", covariant=True) + +class NestedSequence(Protocol[_T_co]): + def __getitem__(self, key: int, /) -> _T_co | NestedSequence[_T_co]: ... + def __len__(self, /) -> int: ... Device = Literal["cpu"] if TYPE_CHECKING or sys.version_info >= (3, 9): -- cgit v1.2.1 From 3952e8f1390629078fdb229236b3b1ce40140c32 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 18 Oct 2021 18:11:06 +0200 Subject: ENH: Change `SupportsDLPack` into a protocol --- numpy/array_api/_typing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'numpy/array_api/_typing.py') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 5e980b16f..dfa87b358 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -67,6 +67,8 @@ if TYPE_CHECKING or sys.version_info >= (3, 9): else: Dtype = dtype -SupportsDLPack = Any SupportsBufferProtocol = Any PyCapsule = Any + +class SupportsDLPack(Protocol): + def __dlpack__(self, /, *, stream: None = ...) -> PyCapsule: ... -- cgit v1.2.1