Skip to content

Commit 4240314

Browse files
committed
Update the docstring of numpy/_array_api/__init__.py
1 parent aee3a56 commit 4240314

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

numpy/_array_api/__init__.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
"""
22
A NumPy sub-namespace that conforms to the Python array API standard.
33
4+
This submodule accompanies NEP 47, which proposes its inclusion in NumPy.
5+
46
This is a proof-of-concept namespace that wraps the corresponding NumPy
57
functions to give a conforming implementation of the Python array API standard
68
(https://data-apis.github.io/array-api/latest/). The standard is currently in
79
an RFC phase and comments on it are both welcome and encouraged. Comments
810
should be made either at https://github.com/data-apis/array-api or at
911
https://github.com/data-apis/consortium-feedback/discussions.
1012
11-
This submodule will be accompanied with a NEP (not yet written) proposing its
12-
inclusion in NumPy.
13-
1413
NumPy already follows the proposed spec for the most part, so this module
1514
serves mostly as a thin wrapper around it. However, NumPy also implements a
1615
lot of behavior that is not included in the spec, so this serves as a
1716
restricted subset of the API. Only those functions that are part of the spec
1817
are included in this namespace, and all functions are given with the exact
1918
signature given in the spec, including the use of position-only arguments, and
2019
omitting any extra keyword arguments implemented by NumPy but not part of the
21-
spec. Note that the array object itself is unchanged, as implementing a
22-
restricted subclass of ndarray seems unnecessarily complex for the purposes of
23-
this namespace, so the API of array methods and other behaviors of the array
24-
object will include things that are not part of the spec.
25-
26-
The spec is designed as a "minimal API subset" and explicitly allows libraries
27-
to include behaviors not specified by it. But users of this module that intend
28-
to write portable code should be aware that only those behaviors that are
29-
listed in the spec are guaranteed to be implemented across libraries.
20+
spec. The behavior of some functions is also modified from the NumPy behavior
21+
to conform to the standard. Note that the underlying array object itself is
22+
wrapped in a wrapper Array() class, but is otherwise unchanged. This submodule
23+
is implemented in pure Python with no C extensions.
24+
25+
The array API spec is designed as a "minimal API subset" and explicitly allows
26+
libraries to include behaviors not specified by it. But users of this module
27+
that intend to write portable code should be aware that only those behaviors
28+
that are listed in the spec are guaranteed to be implemented across libraries.
29+
Consequently, the NumPy implementation was chosen to be both conforming and
30+
minimal, so that users can use this implementation of the array API namespace
31+
and be sure that behaviors that it defines will be available in conforming
32+
namespaces from other libraries.
3033
3134
A few notes about the current state of this submodule:
3235
@@ -45,16 +48,10 @@
4548
not included here, as it requires a full implementation in NumPy proper
4649
first.
4750
48-
- np.argmin and np.argmax do not implement the keepdims keyword argument.
49-
5051
- The linear algebra extension in the spec will be added in a future pull
5152
request.
5253
53-
- Some tests in the test suite are still not fully correct in that they test
54-
all datatypes whereas certain functions are only defined for a subset of
55-
datatypes.
56-
57-
The test suite is yet complete, and even the tests that exist are not
54+
The test suite is not yet complete, and even the tests that exist are not
5855
guaranteed to give a comprehensive coverage of the spec. Therefore, those
5956
reviewing this submodule should refer to the standard documents themselves.
6057
@@ -68,37 +65,45 @@
6865
dtypes and only those methods that are required by the spec, as well as to
6966
limit/change certain behavior that differs in the spec. In particular:
7067
68+
- The array API namespace does not have scalar objects, only 0-d arrays.
69+
Operations in on Array that would create a scalar in NumPy create a 0-d
70+
array.
71+
7172
- Indexing: Only a subset of indices supported by NumPy are required by the
7273
spec. The Array object restricts indexing to only allow those types of
7374
indices that are required by the spec. See the docstring of the
7475
numpy._array_api.Array._validate_indices helper function for more
7576
information.
7677
7778
- Type promotion: Some type promotion rules are different in the spec. In
78-
particular, the spec does not have any value-based casting. Note that the
79-
code to correct the type promotion rules on numpy._array_api.Array is
80-
not yet implemented.
79+
particular, the spec does not have any value-based casting. The
80+
Array._promote_scalar method promotes Python scalars to arrays,
81+
disallowing cross-type promotions like int -> float64 that are not allowed
82+
in the spec. Array._normalize_two_args works around some type promotion
83+
quirks in NumPy, particularly, value-based casting that occurs when one
84+
argument of an operation is a 0-d array.
8185
8286
- All functions include type annotations, corresponding to those given in the
83-
spec (see _types.py for definitions of the types 'array', 'device', and
84-
'dtype'). These do not currently fully pass mypy due to some limitations in
85-
mypy.
87+
spec (see _types.py for definitions of some custom types). These do not
88+
currently fully pass mypy due to some limitations in mypy.
89+
90+
- Dtype objects are just the NumPy dtype objects, e.g., float64 =
91+
np.dtype('float64'). The spec does not require any behavior on these dtype
92+
objects other than that they be accessible by name and be comparable by
93+
equality, but it was considered too much extra complexity to create custom
94+
objects to represent dtypes.
8695
8796
- The wrapper functions in this module do not do any type checking for things
88-
that would be impossible without leaving the _array_api namespace.
97+
that would be impossible without leaving the _array_api namespace. For
98+
example, since the array API dtype objects are just the NumPy dtype objects,
99+
one could pass in a non-spec NumPy dtype into a function.
89100
90101
- All places where the implementations in this submodule are known to deviate
91102
from their corresponding functions in NumPy are marked with "# Note"
92103
comments. Reviewers should make note of these comments.
93104
94105
Still TODO in this module are:
95106
96-
- Implement the spec type promotion rules on the Array object.
97-
98-
- Disable NumPy warnings in the API functions.
99-
100-
- Implement keepdims on argmin and argmax.
101-
102107
- Device support and DLPack support are not yet implemented. These require
103108
support in NumPy itself first.
104109

0 commit comments

Comments
 (0)