Skip to content
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

Closed
gasparattila opened this issue Feb 2, 2022 · 11 comments
Closed

Type incorrectly inferred as Literal #2324

gasparattila opened this issue Feb 2, 2022 · 11 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@gasparattila
Copy link

Environment data

  • Language Server version: 2022.1.5
  • Python version: 3.10.1

Open the following code in VSCode:

def f(x):
    e = 0
    for _ in [0]:
        e += x
        # print(e)
    return e

Hovering over f shows the incorrect type (x) -> Literal[0], even though f(1) == 1. Note that the problem seems to disappear when e is used after e += x, for example, when print(e) is uncommented.

@github-actions github-actions bot added the triage label Feb 2, 2022
@erictraut
Copy link
Contributor

erictraut commented Feb 2, 2022

I'm not able to repro what you're seeing. For me, the inferred type of f is (x) -> (Any | Literal[0]), which is correct in this case.

Screen Shot 2022-02-02 at 12 32 24 PM

Are you sure you're seeing (x) -> Literal[0]?

If you add a type annotation for the input parameter (presumably x is meant to be either an int or float?), then the return type inference is more accurate.

def f(x: int):
    e = 0
    for _ in [0]:
        e += x
        # print(e)
    return e

reveal_type(f) # (x: int) -> int

@gasparattila
Copy link
Author

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 y = f(1) it shows an incorrect value for y.

@erictraut
Copy link
Contributor

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.

@gasparattila
Copy link
Author

I have no idea what is causing the difference. I could reproduce the issue in a private browser window:

Screenshot_20220203_123553

The issue might have been introduced in version 2021.9.2, I get the correct type using 2021.9.1.

@erictraut
Copy link
Contributor

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.

@erictraut
Copy link
Contributor

This will be fixed in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Mar 8, 2022
@bschnurr
Copy link
Member

bschnurr commented Mar 9, 2022

This issue has been fixed in version 2022.3.1, which we've just released. You can find the changelog here: CHANGELOG.md

@bschnurr bschnurr closed this as completed Mar 9, 2022
@gasparattila
Copy link
Author

I can still reproduce the problem in both 2022.3.1 and 2022.3.2. When I hover over only the last e, I still get the wrong type:

Screenshot_20220317_201123

@erictraut
Copy link
Contributor

I've reopened the tracking bug for investigation: microsoft/pyright#2983.

@erictraut
Copy link
Contributor

This will be addressed in the next release.

@erictraut erictraut reopened this Mar 27, 2022
@bschnurr
Copy link
Member

This issue has been fixed in version 2022.3.4, which we've just released. You can find the changelog here: CHANGELOG.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants