-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
What to do about types from the numbers module? #2636
Comments
I looked into this a long time ago and my conclusion was that supporting Also, as |
That sounds about right, and similar to the deliberations I recall we had about this during the formative days of PEP 484. So maybe we can just warn about them? |
Just posting here an older proposal by @JukkaL so that it is not lost python/typing#272 |
Problems with |
Or it could just treat it as an alias for int?
|
Making class MyIntegral(numbers.Integral):
# Mypy would think that this would inherit all `int` methods, even
# though that's not the case at runtime
...
n = MyIntegral(5)
chr(n) # Mypy thinks that this okay even though it would fail at runtime Another concern is that if we support this, some users will likely start using I don't really see the benefits of having the alias, since it would usually not do the right thing. Also, I haven't seen any real-world code that would clearly benefit from |
I think most of |
OK, let's not do anything for numbers right now, and let's consider making them protocols in the future. |
I foresee ugliness even if we'd use protocols, because |
I still think that supporting |
@JukkaL I think it may be possible to make an exception for |
@ilevkivskyi There is the problem that the official docs (https://docs.python.org/2/library/numbers.html#implementing-the-arithmetic-operations) suggest that implementations of operator methods should use checks like The above is just one of the problems I am aware of with If you want to convince me that this will work, I'd like to see a stub definition of subsets of, say,
|
@JukkaL |
I think that this is a complicated problem and a solution would only be situationally useful, so this doesn't seem worth the effort. |
PEP 3141 introduces a numeric tower and a corresponding module
numbers
defining some numeric ABCs likeIntegral
andReal
. These ABCs are ignored by PEP 484 (you just have to useint
orfloat
) but some people don't know that and in their zeal to be hyper-correct use e.g.numbers.Integral
in type annotations. Then type checks fail because mypy doesn't considerint
a subclass ofnumbers.Integral
.We should either make this work or warn about numeric ABCs when used in type annotations.
The text was updated successfully, but these errors were encountered: