Repository navigation
Closed
Labels
Tracking IssueTracking issue.
Milestone
Description
Some issues with type annotations that were brought up by @BvB93 at numpy/numpy#18585
- For
asarray, it isn't clear how to defineNestedSequence(typingannotations don't support recursive types). -
asarrayneeds to also includearrayas a valid type forobj(arrayshould 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
NestedSequencetype inasarray) - For
stackandconcat, the type forarraysshould beTuple[array, ...]instead ofTuple[array] - For
stackandconcat(and possibly others),Tupleis too restrictive vs.Sequence. -
uniquecan be implemented as an@overload(see ENH: Implementation of the NEP 47 (adopting the array API standard) numpy/numpy#18585 (comment)). I don't know if this needs to be changed in the spec. - The return type for
finfoasfinfodoesn't make sense iffinfois a function (see ENH: Implementation of the NEP 47 (adopting the array API standard) numpy/numpy#18585 (comment)). -
__getitem__and__setitem__should include theellipsistype (ENH: Implementation of the NEP 47 (adopting the array API standard) numpy/numpy#18585 (comment)) -
(update:__len__should returnint__len__is slated for removal from the spec; see Specify shape behavior #289) - The return type for
shapeis currentlyUnion[ Tuple[ int, …], <shape> ], which needs to be addressed (there is a TODO in the spec for this). (update: see Specify shape behavior #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(ENH: Implementation of the NEP 47 (adopting the array API standard) numpy/numpy#18585 (comment))? (I don't know if this is relevant for the spec). - The dunder methods (
__add__, and so on) should includeint,float, (andboolas appropriate) as allowed types forother.
Metadata
Metadata
Assignees
Labels
Tracking IssueTracking issue.
Activity
rgommers commentedon Mar 13, 2021
Thanks for the great feedback @BvB93, and for collecting it here @asmeurer. I'll try and fix the straightforward ones now.
It's valid in Python 3.8 with
from __future__ import annotations. And py38 is our minimum version because of positional-only parameters, so we're fine here.No, I don't think that should be changed in the spec, that would just make it harder to read. It's an implementation issue.
The return type isn't relevant, it must be an object with the minimum properties. So the suggestion of using a
typing.Protocolis a good one. I'll think about the best way to include that in the standard.No, this is on purpose. We cannot have valid types for library-specific classes/objects.
<array>means "the conforming array type in the library". We can add atyping.Protocolin the NumPy implementation, and that will then very likely get reused in other libraries. But in a document that doesn't make sense.Same answer, yes in implementation, no in spec.
rgommers commentedon Mar 13, 2021
Also an implementation issue. I'd simply type it as
Sequence[Any]for now, given that there's no canonical way to do it. Note that for NumPy, there's alreadynumpy.typing._NestedSequencewhich has a similar issue.rgommers commentedon Mar 13, 2021
Already covered by
SupportsDLPack, but can add explicitly and specify it's a no-op in that case.EDIT: after reviewing, that actually makes it less clear. It should be treated just like other objects that are covered by
SupportsDLPack- e.g.copy=Falseshares the data, but does create a new array instance (rather than returning the existing input instance).rgommers commentedon Mar 13, 2021
I'm actually not sure
Sequenceis 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 thinkSequenceis.Address some small type annotation issues
Address some small type annotation issues
rgommers commentedon Mar 13, 2021
The annotation wasn't
finfo, it was<class>. To further clarify I changed it to<finfo object>.Address some small type annotation issues (#144)
asmeurer commentedon Mar 31, 2021
Another issue I just noticed. The dunder methods (
__add__, and so on) should includeint,float, (andboolas appropriate) as allowed types forother.BvB93 commentedon Jul 28, 2021
Continuing from numpy/numpy#18585 (comment):
@asmeurer when used as parameter type, all unions involving
Anyare indeed equivalent to aAny(from a typing perspective). I made a similar comment in #185 (comment), but the more verbose union was preferred then for the sake of clarity.The
__dlpack__protocol is currently in a unique situation as the parameter type ofstreamis variable, i.e. different concrete implementations are allowed to specify a bound (as long as this bound involvesNonesomehow). This is similar to, e.g., builtin pythons'__getitem__protocol, where both the key- and return-type are variables.So no, the most correct type for the more general
__dlpack__spec would not beAny, it would be a typevar:None | ~T. For concrete implementations would narrow down to the like ofNone(like for numpy),None | intorNone | int | FancyStreamType.Examples
An example of
__dlpack__represented as atyping.Protocolsubclass.BvB93 commentedon Sep 27, 2021
Two problems recently discovered in numpy/numpy#19969:
*args: Sequence[Array]implies that every individual argument must be a sequence of arrays. I strongly suspect that, in most cases, this does not represent the actual intention (e.g.result_type).eyecurrently acceptsk is None, a value that is not accepted bynp.eye. As was noted by @asmeurer, no mention is made ofNoneoutside of theOptional[int]annotation, so it's not 100% clear if this was intentional or merely an oversight.kgryte commentedon Nov 4, 2021
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.