-
-
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
Pow doesn't infer types when variables are used #16635
Comments
Possible linked issue: #7621 |
Curiously in
I'd think it should be typed |
@ikonst see https://github.com/python/typeshed/blob/ad5ec921e0c0da471867996fc85b94a3e7d83ecf/stdlib/builtins.pyi#L294 for why it's this way. @GatienBouyer the explanation for what you're seeing is that if you put the value in a variable, mypy infers the type as just |
@ikonst, I tried that two years ago, and this is the mypy_primer report! python/typeshed#6287 (comment) |
Indeed, when variables are used, the parameters are of type However, is there no other way to define positive integers? In a way that enables me to type my variables correctly, so I have the correct type inferred for the result. _PositiveInteger: TypeAlias = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] The following would be typed Any even if the value is explicitly positive. x = pow(2, 100)
reveal_type(x) # = Any There seems to be no way to have a large # from mypy.typeshed.stubs.[etc.] import _PositiveInteger # no way to import it
_PositiveInteger = int # to demonstrate the code below
my_2: _PositiveInteger = 2
my_3: _PositiveInteger = 3
would_be_int = pow(my_2, my_3) My main driver here is to improve my Any ratio in the reports produced by mypy, especially the cobertura and html ones. |
not really :/ |
Ahh, I see, e.g. existing code in https://github.com/Yelp/paasta/blob/master/paasta_tools/utils.py: def calculate_tail_lines(verbose_level: int) -> int:
if verbose_level <= 1:
return 0
else:
return 10 ** (verbose_level - 1) Nobody says you can't call |
As already discussed in python/typeshed#7737 (comment) and in python/typeshed#6287 (comment), mypy has no way to distinguish between positive and negative integers. Until further change on this topic occurs, I close this issue. |
I add this reference for completeness. |
I'll note that pyright doesn't have a problem with the original code sample in this issue. That's because pyright narrows to literal types on assignment, whereas mypy does not. Perhaps this is something mypy maintainers would consider changing. Code sample in pyright playground my_wanted_int = 2**3
reveal_type(my_wanted_int) # int
my_2 = 2
my_3 = 3
reveal_type(my_2) # (mypy: int, pyright: Literal[2])
reveal_type(my_3) # (mypy: int, pyright: Literal[3])
star_result2 = my_2**my_3
reveal_type(star_result2) # (mypy: Any, pyright: int) |
Bug Report
I would expect that using variables to hold my values doesn't lose the resulting type.
Sample code:
Actual Behavior
Here is the example in the mypy playground:
https://mypy-play.net/?mypy=latest&python=3.11&gist=e53d4334e01415845108453985c8f88f
Your Environment
The text was updated successfully, but these errors were encountered: