ENH: Make np.complexfloating generic w.r.t. np.floating#17172
ENH: Make np.complexfloating generic w.r.t. np.floating#17172eric-wieser merged 2 commits intonumpy:masterfrom
np.complexfloating generic w.r.t. np.floating#17172Conversation
np.complexfloating generic w.r.t. to np.floatingnp.complexfloating generic w.r.t. np.floating
| @property | ||
| def real(self) -> _FloatType: ... | ||
| @property | ||
| def imag(self) -> _FloatType: ... |
There was a problem hiding this comment.
Aren't these properties of np.number anyway?
There was a problem hiding this comment.
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
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.
I think we forgot that integers support real and imag too, to match python
There was a problem hiding this comment.
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
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.
I think number makes more sense than generic, as the result of str_.real is nonsense
There was a problem hiding this comment.
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.
Why is ignore needed here but not on the others?
There was a problem hiding this comment.
__abs__() is already defined a super-class while the other two are not (as per our previous real/imag discussion):
Line 256 in 32b3f82
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
eric-wieser
left a comment
There was a problem hiding this comment.
Looks fine, we can clean up number.real and number.imag later.
This pull requests makes
np.complexfloatinggeneric with respect tonp.floating.The main advantage of this is to make it easier to access the
.real/.imagtype ofnp.complexfloatingsub-classes as is illustrated below.Examples