ENH: Annotate the arithmetic operations of ndarray and generic#17273
ENH: Annotate the arithmetic operations of ndarray and generic#17273charris merged 27 commits intonumpy:masterfrom
ndarray and generic#17273Conversation
…ing` annotations
Addresses numpy#17273 (comment) The use of `__call__`-defining protocols is not limited to callbacks. The module name name & docstring now reflects this.
Most `np.bytes_` / `np.str_` methods return their builtin `bytes` / `str` counterpart. This includes addition.
Addresses numpy#17273 (comment) Dividing a `np.bool_` by an integer (or vice versa) always returns `float64`
|
Rebased to get rid of a merge conflict. @eric-wieser could you mark the PR as approved if you've got no further comments? |
|
Using |
Good to know, I'll keep it in mind for the future. |
`unsignedinteger + signedinteger` generally returns a `signedinteger` subclass. The exception to this is `uint64 + signedinteger`, which returns `float64`. Addresses numpy#17273 (comment)
|
@eric-wieser Are you OK with this? |
|
I think we've discussed the most important aspects. |
OK, lets put this in, don't want to let too much work stack up :) Thanks @BvB93 . |
This pull requests adds annotations for
ndarrayandgenericarithmetic operations:__add____sub____mul____truediv____floordiv____pow__A few notes about the PR:
While adding annotations to
timedelta64I encountered numerous issue due to its incompatibility with itssignedintegerbaseclass:__index____pow__For this reason its baseclass has been changed from
signedintegertogeneric. While not strictly true during runtime, the problems withtimedelta64inheriting fromsignedintegerhave been noted before (#10685) and this change would be inline with the fix proposed in abovementioned issue.Secondly, one obstacle that hasn't been dealt with is how the handle
generics precision,e.g. how to annotate
int64(...) + float32(...) -> float64(...)without adding massive amounts ofoverloads.I might have an idea about to do deal with this issue, but I'd prefer to save it for a future PR due to some necessary compromises.
In any case, for the time being the return types are generally limited to types with ambiguous precision (
signedinteger,floating,complexfloating, etc...)Finally, in order the deal with the repetitive and
@overload-prone function signatures, this pull request makes extensive use of callback protocols. Callback protocols are effectively more fancy version oftyping.Callableand, therefore , can be assigned and reused.