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 "variable is not accessed" message #107

Closed
tweakimp opened this issue Jul 14, 2020 · 7 comments
Closed

False "variable is not accessed" message #107

tweakimp opened this issue Jul 14, 2020 · 7 comments
Assignees
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@tweakimp
Copy link

Environment data

  • Language Server version: Pylance language server 2020.7.1
  • OS and version: Windows 10 64bit
  • Python version: 3.8.2

Expected behaviour

No message at variable string in line 6, just like in line 13.

Actual behaviour

Variable msg in Test.func is marked as "string" is not accessed, while it is clearly being accessed in the next line.

Code Snippet / Additional information

class Test:
    def func(self):
        try:
            1 / 0
        except ZeroDivisionError as exc:
            string = "A string"  # pylance: "string" is not accessed
            raise exc(string)


try:
    1 / 0
except ZeroDivisionError as exc:
    string = "A string"  # No pylance message here (?)
    raise exc(string)
@judej judej added the bug Something isn't working label Jul 14, 2020
@erictraut
Copy link
Contributor

erictraut commented Jul 14, 2020

Thanks for the bug report.

If you turn on the type checker (i.e. set "python.analysis.typeCheckingMode" to "basic"), you'll see that Pylance finds a bug in the above code. The variable exc contains an instance of ZeroDivisionError, which is not callable. It appears that your code assumes that exc refers to the ZeroDivisionError class, which it doesn't. So your code will crash (i.e. generate another exception) within your exception handler.

That said, Pylance should not mark the symbol string as unreferenced.

@erictraut
Copy link
Contributor

This will be fixed in the next version of Pylance & Pyright.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Jul 14, 2020
@tweakimp
Copy link
Author

tweakimp commented Jul 14, 2020

Thank you for your fast reply.
While you are right, the code was not working as intended, why was that only a problem for pylance in the class version, not also in the other one?

If I use the following code, pylance does not think that string is not accessed.

class Test:
    def func(self):
        try:
            1 / 0
        except ZeroDivisionError as exc:
            string = "A string"
            raise exc.__class__(string)

@erictraut
Copy link
Contributor

The problem wasn't reported for the second version because Pylance assumes that symbols within the module-level scope should never be marked as "not accessed" because they might be improted by other modules. The exception to this rule is if the name of the symbol starts with an underscore, which tells Pylance that it should be private to the module. So if you had named your variable _string rather than string, you would have seen the same behavior for both examples.

@jakebailey
Copy link
Member

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

@smuuf
Copy link

smuuf commented Aug 7, 2020

I'd like to report this/similar issue with version 2020.8.0.

obrazek

Code to reproduce

class WhateverClass:

    def some_method(self):
        some_variable = 123

        return type('Whatever', (object,), {
            'whatever': some_variable,
        })

Pylance version

[Info  - 11:07:14 AM] Pylance language server 2020.8.0 (pyright 4b16a648) starting

Using

PyPy 7.3.1 in WSL1

Python 3.6.9 (7.3.1+dfsg-4~ppa2~ubuntu18.04, Apr 30 2020, 20:59:10)
[PyPy 7.3.1 with GCC 7.5.0]

@erictraut
Copy link
Contributor

@smuuf, thanks for the bug report. This is a different issue that is specific to the type call. It will be fixed in the next version of Pylance.

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

5 participants