Skip to content

ENH: Annotate the arithmetic operations of ndarray and generic#17273

Merged
charris merged 27 commits intonumpy:masterfrom
BvB93:arithmetic2
Oct 2, 2020
Merged

ENH: Annotate the arithmetic operations of ndarray and generic#17273
charris merged 27 commits intonumpy:masterfrom
BvB93:arithmetic2

Conversation

@BvB93
Copy link
Copy Markdown
Member

@BvB93 BvB93 commented Sep 8, 2020

This pull requests adds annotations for ndarray and generic arithmetic operations:

  • __add__
  • __sub__
  • __mul__
  • __truediv__
  • __floordiv__
  • __pow__

A few notes about the PR:

While adding annotations to timedelta64 I encountered numerous issue due to its incompatibility with its signedinteger baseclass:

  • No support for __index__
  • No support for __pow__
  • Does not support operations with complex numbers
  • Most operations do not support floats

For this reason its baseclass has been changed from signedinteger to generic. While not strictly true during runtime, the problems with timedelta64 inheriting from signedinteger have 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 of overloads.
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...)

import numpy as np

i8 = np.int64(...)
f4 = np.float32(...)
f4 + i8  # note: Revealed type is 'numpy.floating'

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 of typing.Callable and, therefore , can be assigned and reused.

Bas van Beek added 20 commits September 16, 2020 23:08
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`
@BvB93
Copy link
Copy Markdown
Member Author

BvB93 commented Sep 16, 2020

Rebased to get rid of a merge conflict.

@eric-wieser could you mark the PR as approved if you've got no further comments?

@BvB93
Copy link
Copy Markdown
Member Author

BvB93 commented Sep 17, 2020

Whoops, wrong branch. Please ignore dbf2018 and c526fb6.

@charris
Copy link
Copy Markdown
Member

charris commented Sep 17, 2020

Using git rebase -i HEAD~n, where n is the number or commits you want to clean up, followed by a force push is usually cleaner than reverting. You can merge/delete commits that way.

@BvB93
Copy link
Copy Markdown
Member Author

BvB93 commented Sep 17, 2020

You can merge/delete commits that way.

Good to know, I'll keep it in mind for the future.

Bas van Beek added 3 commits September 17, 2020 21:41
`unsignedinteger + signedinteger` generally returns a `signedinteger` subclass.
The exception to this is `uint64 + signedinteger`, which returns `float64`.

Addresses numpy#17273 (comment)
@charris
Copy link
Copy Markdown
Member

charris commented Sep 27, 2020

@eric-wieser Are you OK with this?

@BvB93
Copy link
Copy Markdown
Member Author

BvB93 commented Oct 2, 2020

I think we've discussed the most important aspects.
The main thing still on the to-do list is dealing infering the return types' precision, which is intended for a future PR
(e.g. int64 + float32 -> float64 instead of ... -> floating).

@charris
Copy link
Copy Markdown
Member

charris commented Oct 2, 2020

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 .

@charris charris merged commit 47a918f into numpy:master Oct 2, 2020
@BvB93 BvB93 deleted the arithmetic2 branch October 2, 2020 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants