-
Notifications
You must be signed in to change notification settings - Fork 767
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
Type incorrectly inferred as Literal #2324
Comments
I'm not able to repro what you're seeing. For me, the inferred type of Are you sure you're seeing If you add a type annotation for the input parameter (presumably def f(x: int):
e = 0
for _ in [0]:
e += x
# print(e)
return e
reveal_type(f) # (x: int) -> int |
I could reproduce the issue using https://vscode.dev after installing the Python extension. Adding a type annotation does fix the issue. However, it can be confusing in unannotated code, for example, in |
I suspect that you're using an old version of Pylance in vscode.dev. Please go to the extensions panel and click on the Pylance extension to see what version it is. You may see a "reload" button if it's out of date. Once I updated to the latest version of Pylance, I received the expected inferred type within the hover text. |
I was able to repro the problem. It depends on the order in which expressions are evaluated, so it's nondeterministic. Since this is a problem in the core type checker, I've created a tracking bug in the pyright repo: microsoft/pyright#2983. |
This will be fixed in the next release. |
This issue has been fixed in version 2022.3.1, which we've just released. You can find the changelog here: CHANGELOG.md |
I've reopened the tracking bug for investigation: microsoft/pyright#2983. |
This will be addressed in the next release. |
This issue has been fixed in version 2022.3.4, which we've just released. You can find the changelog here: CHANGELOG.md |
Environment data
Open the following code in VSCode:
Hovering over
f
shows the incorrect type(x) -> Literal[0]
, even thoughf(1) == 1
. Note that the problem seems to disappear whene
is used aftere += x
, for example, whenprint(e)
is uncommented.The text was updated successfully, but these errors were encountered: