-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: Make np.complexfloating
generic w.r.t. np.floating
#17172
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
Conversation
np.complexfloating
generic w.r.t. to np.floating
np.complexfloating
generic w.r.t. np.floating
@property | ||
def real(self) -> _FloatType: ... | ||
@property | ||
def imag(self) -> _FloatType: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't these properties of np.number
anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are and they were already defined for np.number
some time ago
Hang on, for some reason this _real_generic
mixing is subclassed in a number of np.number
sub-classes but not np.number
itself. This doesn't seem right.
Lines 381 to 387 in 32b3f82
class _real_generic(generic): # type: ignore | |
@property | |
def real(self: _ArraySelf) -> _ArraySelf: ... | |
@property | |
def imag(self: _ArraySelf) -> _ArraySelf: ... | |
class number(generic): ... # type: ignore |
What makes np.complexfloating
somewhat unique is that the return type of
.real
and .imag
is not type(self)
; it's one of the np.floating
subclasses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we forgot that integers support real and imag too, to match python
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we forgot that integers support real and imag too, to match python
I think we're safe here actually, as _real_generic
is included in the definition of integer
:
Line 407 in 32b3f82
class integer(number, _real_generic): ... # type: ignore |
Still, it would in my opinion be cleaner to just move real
and imag
to the np.generic
superclass.
As both properties have to be redefined for np.complexfloating
anyway (due to their distinct return type)
the original concern should not be an issue (numpy/numpy-stubs#14 (comment)).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think number makes more sense than generic, as the result of str_.real is nonsense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might for the better, yes.
I had to double check but real
and imag
will apparently be deprecated for non-numeric types anyway (#10818).
In any case, I'm planning to save this for a future maintenance pull request as there are also a few other issues I'd like to fix in one go (e.g. str
and bytes
are missing from a couple of __init__()
s).
I don't understand, should that just be
|
Ah nevermind, I understand now |
def real(self) -> _FloatType: ... | ||
@property | ||
def imag(self) -> _FloatType: ... | ||
def __abs__(self) -> _FloatType: ... # type: ignore[override] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is ignore
needed here but not on the others?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__abs__()
is already defined a super-class while the other two are not (as per our previous real
/imag
discussion):
Line 256 in 32b3f82
def __abs__(self: _ArraySelf) -> _ArraySelf: ... |
Mypy will complain due to incompatibilities between the super- and sub-types' signatures,
complaints which are silenced by # type: ignore[override]
.
For more details: https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine, we can clean up number.real
and number.imag
later.
This pull requests makes
np.complexfloating
generic with respect tonp.floating
.The main advantage of this is to make it easier to access the
.real
/.imag
type ofnp.complexfloating
sub-classes as is illustrated below.Examples