-
Notifications
You must be signed in to change notification settings - Fork 765
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
Wrong type of exponentiation with integer base and power #1259
Comments
The problem is that negative exponents make the result I special cased the literal 2 to help with this (as that's the most common power), but you've made a bunch of snippets that aren't that special case: python/typeshed#4473 |
I'm not really sure there's a good way around this, besides special casing every literal integer (which has its tradeoffs). Note that you can annotate your code if you're certain that you're correct: x: int = 10**10 |
True for the stubs, but since pylance is not simply a collection of stubs, is there any chance for the type checker to at least distinguish negative from positive literals? |
Doing so would mean that we're no longer following the typing PEPs (we do what the stubs say), but it does appear as though mypy special cases this: https://mypy-play.net/?mypy=latest&python=3.9&gist=fdab7fc65290a63d922ba2b50b70f38b reveal_type(10**2)
reveal_type(10**3)
reveal_type(10**-1)
def return_int(x: int) -> int:
return x
reveal_type(10**return_int(2))
|
Though, not in a particularly sensible way... https://mypy-play.net/?mypy=latest&python=3.9&gist=fc3a42659210f6c87a96f56ecf965a91 from typing import Literal
def return_10() -> Literal[10]: return 10
def return_neg1() -> Literal[-1]: return -1
reveal_type(10**return_10())
reveal_type(10**return_neg1())
|
I don't think we should special case this in the type checker. This is not the only place where the type system cannot describe the semantics of a call exactly, and adding special cases in the type checker is a slippery slope that we should not go down. The current solution (a return type of Any) is a good compromise because it eliminates false positives in this case. |
Closing old issue. If this is still a problem, please reopen with the information requested. thanks |
Wrong type inference of integer raised to power of any integer other than literal
2
Environment data
Expected behaviour
Type of both
a
,b
andc
should be inferred asint
.Actual behaviour
The text was updated successfully, but these errors were encountered: