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

Unjustified(?) unbound var warning in try exception statements #913

Closed
abelcheung opened this issue Feb 4, 2021 · 4 comments
Closed

Unjustified(?) unbound var warning in try exception statements #913

abelcheung opened this issue Feb 4, 2021 · 4 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

@abelcheung
Copy link

Environment data

  • Language Server version: 2021.2.0 or newer
  • OS and version: XXX
  • Python version (& distribution if applicable, e.g. Anaconda): XXX

Issue

Code sample below would cause Pylance to issue type check error, please see screenshot further down. I think this is a regression, since Pylance 2021.1.3 passes checking for this code snippet.

Code Sample

try:
    # any statement will do
    _ = 'text'.index('a')
except:
    var = 1
else:
    var = 2
finally:
    print(var)

Screenshot

圖片

@github-actions github-actions bot added the triage label Feb 4, 2021
@erictraut
Copy link
Contributor

I agree this is a false positive error. It arises from a fix for another class of false positives that previously occurred with try/except/else/finally statements.

In this very specific case, where there is both an "else" clause and a "bare except" clause (i.e. an except clause with no specified exception type) and a finally clause, we can avoid this particular false positive. I'll make the change to fix this specific case. I'll point out that if you add an exception type to the except clause (e.g. except NameError:), then this is a legitimate error because an exception of a different type could directly execute the finally clause.

@abelcheung
Copy link
Author

Indeed you're correct about the other non-corner cases. Thanks for the prompt triaging!

@erictraut
Copy link
Contributor

This will be fixed in the next release of pylance.

Here's the PR in case you're interested: microsoft/pyright@5bf5640. I used your sample in the unit test. :)

Thanks for the bug report.

@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 triage labels Feb 4, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.2.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202121-10-february-2021

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

3 participants