No message at variable string in line 6, just like in line 13.
Variable msg in Test.func is marked as "string" is not accessed, while it is clearly being accessed in the next line.
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)
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.

class WhateverClass:
def some_method(self):
some_variable = 123
return type('Whatever', (object,), {
'whatever': some_variable,
})
[Info - 11:07:14 AM] Pylance language server 2020.8.0 (pyright 4b16a648) starting
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.
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
_stringrather thanstring, you would have seen the same behavior for both examples.