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

No error reported when nonlocal variable is annotated as global then used #429

Closed
munael opened this issue Sep 26, 2020 · 2 comments
Closed
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@munael
Copy link

munael commented Sep 26, 2020

Environment data

  • Language Server version: 2020.9.6
  • OS and version: Windows Server 2019 Datacenter 1809
  • Python version (& distribution if applicable, e.g. Anaconda): Anaconda, Python 3.7.6 64bit

Expected behaviour

Variables outside a nested function cannot be referenced unless annotated as nonlocal.

Actual behaviour

global works just as fine. Even though no global variable has the name or type that shows up in the hint. It's seeing the nonlocal variable.

Logs

Lots of this:

Background analysis message: setFileOpened
Background analysis message: markFilesDirty
Background analysis message: getSemanticTokens
[BG(1)] parsing: Untitled-2 (2ms)
[BG(1)] binding: Untitled-2 (0ms)
Background analysis message: analyze
[BG(1)] analyzing: Untitled-2 ...
[BG(1)]   checking: Untitled-2 (0ms)
[BG(1)] analyzing: Untitled-2 (0ms)

Code Snippet / Additional information

global_foo : int = 23

def func():
    nonlocal_foo : str = ''
    def func_1():
        global nonlocal_foo
        nonlocal_foo = 23

image

@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release of Pylance.

There is nothing technically wrong with your code sample. It runs without an exception in the Python interpreter, and Pylance should not generate any error for this example.

The bug here was specifically with the case where the global keyword referenced a symbol that was defined in a nonlocal (but also not global) scope.

@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 labels Sep 27, 2020
@github-actions github-actions bot removed the triage label Sep 27, 2020
@jakebailey
Copy link
Member

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

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