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

Pylance incorrectly tagging ReportUndefinedVariable when using global keyword #526

Closed
lazarillo opened this issue Oct 27, 2020 · 3 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

@lazarillo
Copy link

lazarillo commented Oct 27, 2020

Environment data

  • Language Server version: Pylance language server 2020.10.2 (pyright d6db7c90)
  • OS and version: Mac OS X 10.15.7 (Catalina)
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.5 using Miniconda and Clang 10.0.1

Expected behaviour

When using the global keyword anywhere, PyLance should recognize that is a variable declaration.

Actual behaviour

Using the global keyword itself creates the erroneous reportUndefinedVariable error.

Logs

Logs seem irrelevant for this, but I can capture them if needed.

Code Snippet / Additional information

import os
from dotenv import load_dotenv, find_dotenv
try:
    load_dotenv(find_dotenv())
    global MY_SECRET
    MY_SECRET = os.getenv("SavedSecret")
except:
    print("Unable to find environment file secret 'SavedSecret'")

I tried to make this example as generic as possible, but also showing a valid case for the otherwise frowned upon global keyword. I might want to access protected secrets in a way that keeps them out of version control, but flags me, the user, when things go wrong.

In PyLance, the line global MY_SECRET generates the error "MY_SECRET" is not defined [Pylance (reportUndefinedVariable)]. This is obviously not an error. But I would argue that there are cases, such as shown above, where it isn't even bad practice.

@erictraut
Copy link
Contributor

Thanks for the bug report. 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 triage labels Oct 27, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.10.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#2020103-28-october-2020

@lazarillo
Copy link
Author

Awesome, thanks @jakebailey and @erictraut ! I'm loving Pylance! I have a colleague who still says, "Yeah, but PyCharm is better." I feel like Pylance makes VS Code almost as good for Python as PyCharm, but it can also do so much more. :)

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