Skip to content

Commit f801759

Browse files
authored
Merge pull request #21 from data-apis/assumptions
Add sections on Assumptions and signatures
2 parents b79b93b + 28b1964 commit f801759

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Function and method signatures
2+
3+
Function signatures in this standard adhere to the following:
4+
5+
1. Positional parameters must be
6+
[positional-only](https://www.python.org/dev/peps/pep-0570/) parameters.
7+
Positional-only parameters have no externally-usable name. When a function
8+
accepting positional-only parameters is called, positional arguments are
9+
mapped to these parameters based solely on their order.
10+
11+
_Rationale: existing libraries have incompatible conventions, and using names
12+
of positional parameters is not normal/recommended practice._
13+
14+
2. Optional parameters must be
15+
[keyword-only](https://www.python.org/dev/peps/pep-3102/) arguments.
16+
17+
_Rationale: this leads to more readable code, and it makes it easier to
18+
evolve an API over time by adding keywords without having to worry about
19+
keyword order._
20+
21+
3. For functions that have a single positional array parameter, that parameter
22+
is called `x`. For functions that have multiple array parameters, those
23+
parameters are called `xi` with `i = 1, 2, ...` (i.e., `x1`, `x2`).
24+
25+
4. Type annotations are left out of the signatures themselves for readability;
26+
they are added to the descriptions of individual parameters however. In code
27+
which aims to adhere to the standard, adding type annotations is strongly
28+
recommended.
29+
30+
A function signature and description will look like:
31+
32+
```
33+
funcname(x1, x2, /, *, key1=-1, key2=None)
34+
35+
Parameters
36+
37+
x1 : array
38+
description
39+
x2 : array
40+
description
41+
key1 : int
42+
description
43+
key2 : Optional[str]
44+
description
45+
46+
Returns
47+
48+
out : array
49+
description
50+
```
51+
52+
Method signatures will follow the same conventions and, modulo the addition of
53+
`self`.

spec/API_specification/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ API specification
55
:caption: API specification
66
:maxdepth: 1
77

8+
function_and_method_signatures
89
array_object
910
indexing
1011
data_types
Loading

spec/assumptions.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,74 @@
11
# Assumptions
22

3-
## Hardware environments
3+
## Hardware and software environments
44

5+
No assumptions on a specific hardware environment are made. It must be possible
6+
to create an array library adhering to this standard that runs (efficiently) on
7+
a variety of different hardware: CPUs with different architectures, GPUs,
8+
distributed systems and TPUs and other emerging accelerators.
59

10+
The same applies to software environments: it must be possible to create an
11+
array library adhering to this standard that runs efficiently independent of
12+
what compilers, build-time or run-time execution environment, or distribution
13+
and install method is employed. Parallel execution, JIT compilation, and
14+
delayed (lazy) evaluation must all be possible.
615

7-
## Software environments
8-
16+
The variety of hardware and software environments puts _constraints_ on choices
17+
made in the API standard. For example, JIT compilers may require output dtypes
18+
of functions to be predictable from input dtypes only rather than input values.
919

1020

1121
## Dependencies
1222

23+
The only dependency that's assumed in this standard is that on Python itself.
24+
Python >= 3.8 is assumed, motivated by the use of positional-only parameters
25+
(see [function and method signatures](API_specification/function_and_method_signatures.md)).
26+
27+
Importantly, array libraries are not assumed to be aware of each other, or of
28+
a common array-specific layer. The [use cases](use_cases.md) do not require
29+
such a dependency, and building and evolving an array library is easier without
30+
such a coupling. Facilitation support of multiple array types in downstream
31+
libraries is an important use case however, the assumed dependency structure
32+
for that is:
33+
34+
![dependency assumptions diagram](_static/images/dependency_assumption_diagram.png)
35+
36+
Array libraries may know how to interoperate with each other, for example by
37+
constructing their own array type from that of another library or by shared
38+
memory use of an array (see [Data interchange mechanisms](design_topics/data_interchange.md)).
39+
This can be done without a dependency though - only adherence to a protocol is
40+
enough.
41+
42+
Array-consuming libraries will have to depend on one or more array libraries.
43+
That could be a "soft dependency" though, meaning retrieving an array library
44+
namespace from array instances that are passed in, but not explicitly doing
45+
`import arraylib_name`.
46+
47+
48+
## Backwards compatibility
49+
50+
The assumption made during creation of this standard is that libraries are
51+
constrained by backwards compatibility guarantees to their users, and are
52+
likely unwilling to make significant backwards-incompatible changes for the
53+
purpose of conforming to this standard. Therefore it is assumed that the
54+
standard will be made available in a new namespace within each library, or the
55+
library will provide a way to retrieve a module or module-like object that
56+
adheres to this standard. See [How to adopt this API](purpose_and_scope.html#how-to-adopt-this-api)
57+
for more details.
58+
59+
60+
## Production code & interactive use
1361

62+
It is assumed that the primary use case is writing production code, for example
63+
in array-consuming libraries. As a consequence, making it easy to ensure that
64+
code is written as intended and has unambiguous semantics is preferred - and
65+
clear exceptions must be raised otherwise.
1466

15-
## Interactive use & production code
67+
It is also assumed that this does not significantly detract from the
68+
interactive user experience. However, in case existing libraries differ in
69+
behavior, the more strict version of that behavior is typically preferred. A
70+
good example is array inputs to functions - while NumPy accepts lists, tuples,
71+
generators, and anything else that could be turned into an array, most other
72+
libraries only accept their own array types. This standard follows the latter choice.
73+
It is likely always possible to put a thin "interactive use convenience layer"
74+
on top of a more strict behavior.

0 commit comments

Comments
 (0)