A NumPy sub-namespace that conforms to the Python array API standard.
This submodule accompanies NEP 47, which proposes its inclusion in NumPy. It is still considered experimental, and will issue a warning when imported.
This is a proof-of-concept namespace that wraps the corresponding NumPy functions to give a conforming implementation of the Python array API standard (https://data-apis.github.io/array-api/latest/). The standard is currently in an RFC phase and comments on it are both welcome and encouraged. Comments should be made either at https://github.com/data-apis/array-api or at https://github.com/data-apis/consortium-feedback/discussions.
NumPy already follows the proposed spec for the most part, so this module serves mostly as a thin wrapper around it. However, NumPy also implements a lot of behavior that is not included in the spec, so this serves as a restricted subset of the API. Only those functions that are part of the spec are included in this namespace, and all functions are given with the exact signature given in the spec, including the use of position-only arguments, and omitting any extra keyword arguments implemented by NumPy but not part of the spec. The behavior of some functions is also modified from the NumPy behavior to conform to the standard. Note that the underlying array object itself is wrapped in a wrapper Array() class, but is otherwise unchanged. This submodule is implemented in pure Python with no C extensions.
The array API spec is designed as a "minimal API subset" and explicitly allows libraries to include behaviors not specified by it. But users of this module that intend to write portable code should be aware that only those behaviors that are listed in the spec are guaranteed to be implemented across libraries. Consequently, the NumPy implementation was chosen to be both conforming and minimal, so that users can use this implementation of the array API namespace and be sure that behaviors that it defines will be available in conforming namespaces from other libraries.
A few notes about the current state of this submodule:
There is a test suite that tests modules against the array API standard at https://github.com/data-apis/array-api-tests. The test suite is still a work in progress, but the existing tests pass on this module, with a few exceptions:
The test suite is not yet complete, and even the tests that exist are not guaranteed to give a comprehensive coverage of the spec. Therefore, when reviewing and using this submodule, you should refer to the standard documents themselves. There are some tests in numpy.array_api.tests, but they primarily focus on things that are not tested by the official array API test suite.
There is a custom array object, numpy.array_api.Array, which is returned by all functions in this module. All functions in the array API namespace implicitly assume that they will only receive this object as input. The only way to create instances of this object is to use one of the array creation functions. It does not have a public constructor on the object itself. The object is a small wrapper class around numpy.ndarray. The main purpose of it is to restrict the namespace of the array object to only those dtypes and only those methods that are required by the spec, as well as to limit/change certain behavior that differs in the spec. In particular:
All functions include type annotations, corresponding to those given in the spec (see _typing.py for definitions of some custom types). These do not currently fully pass mypy due to some limitations in mypy.
Dtype objects are just the NumPy dtype objects, e.g., float64 = np.dtype('float64'). The spec does not require any behavior on these dtype objects other than that they be accessible by name and be comparable by equality, but it was considered too much extra complexity to create custom objects to represent dtypes.
All places where the implementations in this submodule are known to deviate from their corresponding functions in NumPy are marked with "# Note:" comments.
Still TODO in this module are:
Module | _array_object |
Wrapper class around the ndarray object for the array API standard. |
Module | _constants |
Undocumented |
Module | _creation_functions |
No module docstring; 15/17 functions documented |
Module | _data_type_functions |
No module docstring; 6/7 functions, 0/2 class documented |
Module | _dtypes |
Undocumented |
Module | _elementwise_functions |
No module docstring; 56/56 functions documented |
Module | _manipulation_functions |
No module docstring; 8/8 functions documented |
Module | _searching_functions |
No module docstring; 4/4 functions documented |
Module | _set_functions |
No module docstring; 3/4 functions, 0/3 class documented |
Module | _sorting_functions |
No module docstring; 2/2 functions documented |
Module | _statistical_functions |
Undocumented |
Module | _typing |
This file defines the types for type annotations. |
Module | _utility_functions |
No module docstring; 2/2 functions documented |
Module | linalg |
No module docstring; 19/24 functions, 0/4 class documented |
Module | setup |
Undocumented |
Package | tests |
Tests for the array API namespace. |