microsoft / pyright

Static Type Checker for Python
Other
13.47k stars 1.48k forks source link

False positive reportPossiblyUnboundVariable with dynamic or lazy imports #9494

Closed ssbarnea closed 7 hours ago

ssbarnea commented 12 hours ago

Describe the bug

Apparently pyright is not able to detect any conditional imports. This is a big deal as there is a lot of production code that needs to make use of these. For example mypy has no problem dealing with these.

if "run" not in globals():
    from subprocess import run

run("echo 123")
 error: "run" is possibly unbound (reportPossiblyUnboundVariable)

pyright 1.1.389

erictraut commented 7 hours ago

Pyright is working as intended here, so I don't consider this a bug.

In your code sample above, the symbol run is not defined prior to the if statement. Perhaps your sample is incomplete? How would run receive a value prior to the import statement otherwise?