-
Notifications
You must be signed in to change notification settings - Fork 53
Add section on static typing #62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.. array-object: | ||
|
||
# Array object |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
# Static typing | ||
|
||
Good support for static typing both in array libraries and array-consuming | ||
code is desirable. Therefore the exact type or set of types for each | ||
parameter, keyword and return value is specified for functions and methods - | ||
see :ref:`function-and-method-signatures`. That section specifies arrays | ||
simply as `array`; what that means is dealt with in this section. | ||
|
||
Introducing type annotations in libraries became more relevant only when | ||
Python 2.7 support was dropped at the start of 2020. As a consequence, using | ||
type annotations with array libraries is largely still a work in progress. | ||
This version of the API standard does not deal with trying to type _array | ||
properties_ like shape, dimensionality or dtype, because that's not a solved | ||
problem in individual array libraries yet. | ||
|
||
An `array` type annotation can mean either the type of one specific array | ||
object, or some superclass or typing Protocol - as long as it is consistent | ||
with the array object specified in :ref:`array-object`. To illustrate by | ||
example: | ||
|
||
```python | ||
# `Array` is a particular class in the library | ||
def sin(x: Array, / ...) -> Array: | ||
... | ||
``` | ||
|
||
and | ||
|
||
```python | ||
# There's some base class `_BaseArray`, and there may be multiple | ||
# array subclasses inside the library | ||
A = TypeVar('A', bound=_BaseArray) | ||
def sin(x: A, / ...) -> A: | ||
... | ||
``` | ||
should both be fine. There may be other variations possible. Also note that | ||
this standard does not require that input and output array types are the same | ||
(they're expected to be defined in the same library though). Given that | ||
array libraries don't have to be aware of other types of arrays defined in | ||
other libraries (see :ref:`assumptions-dependencies`), this should be enough | ||
for a single array library. | ||
|
||
That said, an array-consuming library aiming to support multiple array types | ||
may need more - for example a protocol to enable structural subtyping. This | ||
API standard currently takes the position that it does not provide any | ||
reference implementation or package that can or should be relied on at | ||
runtime, hence no such protocol is defined here. This may be dealt with in a | ||
future version of this standard. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.