-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add special case for integers raised to a power of 2 #4473
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
Oops, I'm missing the actual Two questions I should ask:
|
The more specific overload must be first because the first overload that matches gets picked. If the return type is int regardless of the value of modulo, I don't see why you shouldn't add the modulo param to the Literal[2] overload. |
Done. I'm thinking this is okay as-is, if you're willing to let |
Unfortunately there's a mypy self-test failure because some error message needs to be adjusted. I can deal with it, but if you have time it would be helpful to make a PR to mypy to update the message. |
Hm, isn't this a chicken/egg problem, where mypy needs to be updated to have this new error, but typeshed can't merge until mypy accepts it? I can take a look later, I've never touched |
Yes it is, so we'll have to merge the two PRs concurrently, which is why it's annoying to do. :) |
|
It's a slippery slope if more were to be added. When I ran a quick search of a bunch of projects I have checked out for testing, |
Forgive me for not updating this; I plan to get back into this / what needs to change in mypy on Friday when I have a bit more time. |
I've opened python/mypy#9415 to update that self test. |
Per: #4352 (comment)
In general, you can't say that the return value of
__pow__
is definitely an integer when called on an int, as a negative power means it returns a float. However, it's pretty common to just writex ** 2
, moreso than any other literal. Add an overload that recognizes that any int to the second is still an int.(Sorry to have taken so long to submit this.)