Some issues with type annotations
See original GitHub issueSome issues with type annotations that were brought up by @Bvb93 at https://github.com/numpy/numpy/pull/18585
- For
asarray
, it isn’t clear how to defineNestedSequence
(typing
annotations don’t support recursive types). -
asarray
needs to also includearray
as a valid type forobj
(array
should not be considred a subtype ofNestedSequence
) - The pipe operator for types isn’t valid yet (https://www.python.org/dev/peps/pep-0604/) (it’s used in the
NestedSequence
type inasarray
) - For
stack
andconcat
, the type forarrays
should beTuple[array, ...]
instead ofTuple[array]
- For
stack
andconcat
(and possibly others),Tuple
is too restrictive vs.Sequence
. -
unique
can be implemented as an@overload
(see https://github.com/numpy/numpy/pull/18585#discussion_r591757472). I don’t know if this needs to be changed in the spec. - The return type for
finfo
asfinfo
doesn’t make sense iffinfo
is a function (see https://github.com/numpy/numpy/pull/18585#discussion_r591763368). -
__getitem__
and__setitem__
should include theellipsis
type (https://github.com/numpy/numpy/pull/18585#discussion_r592256384) -
(update:__len__
should returnint
__len__
is slated for removal from the spec; see https://github.com/data-apis/array-api/pull/289) - The return type for
shape
is currentlyUnion[ Tuple[ int, …], <shape> ]
, which needs to be addressed (there is a TODO in the spec for this). (update: see https://github.com/data-apis/array-api/pull/289) -
Some of the other types for properties also use<...>
types. We should make all the type annotations be valid Python/mypy that can be used in the annotations in the signature. -
Should the array object subclass fromtyping.Protocol
(https://github.com/numpy/numpy/pull/18585#discussion_r592261549)? (I don’t know if this is relevant for the spec). - The dunder methods (
__add__
, and so on) should includeint
,float
, (andbool
as appropriate) as allowed types forother
.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Issue about type annotation - microsoft/pylance-release - GitHub
I think we should allow type modification after function returns. This is essential for Union and subclass. See 2 cases below: from typing ......
Read more >Common issues and solutions - mypy 0.991 documentation
Functions that do not have any annotations (neither for any argument nor for the return type) are not type-checked, and even the most...
Read more >Understanding type annotation in Python - LogRocket Blog
In this extensive post with specific examples, learn how to use Python type annotation to your advantage using the mypy library.
Read more >Type Annotation in Python | Towards Data Science
Type annotations — also known as type signatures — are used to indicate the datatypes of variables and input/outputs of functions and methods....
Read more >Using Type Annotations to Improve Your Code
Survey: Did you attend the tutorial? The locking talk? Which of these best describes you? ○ Specific question / concern / feedback. ○...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m actually not sure
Sequence
is a good idea. It can be quite hard to deal with this correctly in both Python and C/C++, see e.g. https://stackoverflow.com/questions/43566044/what-is-pythons-sequence-protocol. I remember a lot of cases where we special-cased tuple and list inputs, and other kinds of sequences were doing the wrong thing.I can be convinced that accepting
Union[Tuple[<array>, ...], List[<array>]]
is a good idea, but I don’t thinkSequence
is.As all items in this tracking issue have been addressed, will close. Any further issues can be discussed in a new issue and resolved in follow-up PRs.