Pylance-release: False "variable is not accessed" message

Created on 14 Jul 2020  路  7Comments  路  Source: microsoft/pylance-release

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)
bug fixed in next version

Most helpful comment

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.

All 7 comments

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.

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

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)

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.

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

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]

@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.

Was this page helpful?
0 / 5 - 0 ratings