Static conditional evaluation treats a parameter named `sys` or `os` as the module
Describe the bug
Static conditional evaluation applies to sys.version_info, sys.platform and os.name when sys or os is a parameter that shadows the imported module. The condition evaluates statically to false, so pyright doesn't analyze the branch.
The sys forms are evaluated only when the file imports sys. The os.name form is evaluated for any name os, even when the file doesn't import os.
Code or Screenshots
With pythonVersion 3.13 and pythonPlatform Linux:
import os
import sys
class FakeSys:
version_info: tuple[int, ...] = (2, 7)
platform: str = "win32"
class FakeOs:
name: str = "nt"
def func(sys: FakeSys, os: FakeOs) -> None:
if sys.version_info < (3, 0):
reveal_type(sys)
if sys.version_info[0] < 3:
reveal_type(sys)
if sys.platform == "win32":
reveal_type(sys)
if os.name == "nt":
reveal_type(os)
0 errors, 0 warnings, 0 informations
Without import os:
class FakeOs:
name: str = "nt"
def func(os: FakeOs) -> None:
if os.name == "nt":
reveal_type(os)
0 errors, 0 warnings, 0 informations
VS Code extension or command-line
Command-line, pyright 1.1.414
This also affects typing.TYPE_CHECKING and typing_extensions.TYPE_CHECKING:
import typing
import typing_extensions
class FakeTyping:
TYPE_CHECKING = False
def func1(typing: FakeTyping) -> None:
if typing.TYPE_CHECKING:
pass
else:
reveal_type(typing)
def func2(typing_extensions: FakeTyping) -> None:
if typing_extensions.TYPE_CHECKING:
pass
else:
reveal_type(typing_extensions)
0 errors, 0 warnings, 0 informations
Fixed in https://github.com/microsoft/pyright/pull/12035. The binder now filters the file-level sys/os import-alias list by the current scope, so a parameter shadowing the module no longer gets statically folded, and os.name is no longer folded when the file never imports os. Regression tests added (staticExpression3.py / staticExpression4.py, StaticExpressionShadowedModules); the new test fails on the pre-change code (Expected 8 infos, got 0) and passes after the fix. Existing StaticExpression1/StaticExpression2 samples and aliased imports like import sys as _sys still work.