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

False positive for undefined variable #977

Closed
memeplex opened this issue Feb 22, 2021 · 4 comments
Closed

False positive for undefined variable #977

memeplex opened this issue Feb 22, 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

@memeplex
Copy link

memeplex commented Feb 22, 2021

Environment data

  • Language Server version: v2021.2.3
  • OS and version: MacOS 11.2
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.6

Expected behaviour

Given a module test_module in the python path defining an object test_object the following code correctly defines to as a global variable of the current module accessible from the scope of function f and referencing test_object.

def f():
    global to
    from test_module import test_object as to

    to.x = 20

Actual behaviour

Pylance reports undefined variable for to. Removing the global declaration "fixes" it, but I want to to:

  1. Be global
  2. Be imported upon f invocation, not before.

Besides it's valid code, despite the somewhat convoluted use case.

@memeplex
Copy link
Author

memeplex commented Feb 22, 2021

Notice that the import renaming is not necessary, it also happens for:

def f():
    global test_object
    from test_module import test_object

    test_object.x = 20

What is critical is the global declaration. I assume the problem is that PyLance is unable to detect that the variable is correctly defined at the source module, although it's not defined at the current module's global scope.

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

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

@memeplex
Copy link
Author

Thanks!

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