microsoft / pyright

Static Type Checker for Python
Other
13.32k stars 1.45k forks source link

reportUnusedVariable considers variable as used after reassignment #8806

Closed solutionseekeras closed 2 months ago

solutionseekeras commented 2 months ago

Describe the bug

Pyright considers a variable to be used if it has been used at least once, even if it is later reassigned to without use.

Code or Screenshots


def f() -> None:
    a = 1

    # I would expect the left-hand side `a` to be flagged as unused here (PyCharm does so)
    a = 2 * a

# pyright: reportUnusedVariable=error

pyright <path-to-file> results in 0 errors, 0 warnings, 0 informations.

VS Code extension or command-line pyright 1.1.377 on the command-line.

erictraut commented 2 months ago

This isn't how reportUnusedVariable works in pyright. It is reported if the variable is not consumed anywhere in the function.

Pyright is working as designed here, so closing the issue.

solutionseekeras commented 2 months ago

Sounds fair, thanks for the quick response.

Are there any plans to add detection of this? If not, what's the reasoning?