Skip to content
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

numbers module #11863

Open
Gouvernathor opened this issue May 5, 2024 · 2 comments
Open

numbers module #11863

Gouvernathor opened this issue May 5, 2024 · 2 comments
Labels
stubs: incomplete Annotations or sub-modules missing from an existing package or module

Comments

@Gouvernathor
Copy link

image
The signature of Rational.__truediv__ is marked as the following:
image
First of, _ComplexLike sounds like it should be numbers.Complex. But second, the signature should be more refined :

  • the divisor cannot be just any value, it has to be a number, so at a minimum numbers.Complex instead of Unknown. The typing signature should be (self, Complex) -> Complex.
  • when passing a Real as a divisor, the result cannot have an imaginary part and must be Real too. I'd go as far as saying that a Rational divisor would yield a Rational dividend. In any case, a (self, Real) -> Real overload should be defined. And as a bonus, a (self, Rational) -> Rational one too.
@srittau srittau added the stubs: incomplete Annotations or sub-modules missing from an existing package or module label May 5, 2024
@srittau
Copy link
Collaborator

srittau commented May 5, 2024

Please note that your IDE's understanding of the types is a poor representation of how the types are annotated in typeshed. Rational has no annotations for __truediv__ at the moment, instead the signature is inherited from Complex:

@abstractmethod
def __truediv__(self, other) -> _ComplexLike: ...

other isn't annotated at all, at the moment. See also the note about _ComplexLike at the beginning of the file:

# Use _ComplexLike, _RealLike and _IntegralLike for return types in this module
# rather than `numbers.Complex`, `numbers.Real` and `numbers.Integral`,
# to avoid an excessive number of `type: ignore`s in subclasses of these ABCs
# (since type checkers don't see `complex` as a subtype of `numbers.Complex`,
# nor `float` as a subtype of `numbers.Real`, etc.)

We of course accept any improvements in this area (and everywhere else)!

@Gouvernathor
Copy link
Author

Gouvernathor commented May 5, 2024

Well, my type checker (pyright) does consider float as a suclass of Real, so the way it works seems right to me considering the information that it's given.
In any case, I can (eventually) submit a merge request with basically what I proposed but with you local types instead of the official numbers ones, and also applied to the other operations. But I'm not a typing virtuoso so it will need a second look.

But having thought a little about it, maybe an implementation of Rational that would return a complex from the division of two rationals may fit in the python bounds of the numbers interface. Hell, it may even make some mathematical sense for all I know.
What I was talking about is the behavior of float, int, Decimal and Fractions but it may be a limitation on Rational at-large.
I'll be asking about it on the Python Discourse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: incomplete Annotations or sub-modules missing from an existing package or module
Projects
None yet
Development

No branches or pull requests

2 participants