You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Names in unreachable code (version, platform or TYPE_CHECKING branches) become a new local N each time #232
Every name inside code that pyright considers unreachable is emitted as a fresh local N, a new symbol at each occurrence. This covers the branch of a sys.version_info or sys.platform check that doesn't match the indexing environment, the else of if TYPE_CHECKING:, and code after return. References to imported functions and module variables in those branches are lost.
from demo.lib import helper
# ^^^^^^ reference demo 0.1.0 `demo.lib`/helper().
a = 1
# ^ definition demo 0.1.0 `demo.gated`/a.
a = helper()
# ^ reference local 0
# ^^^^^^ reference local 1
b = helper()
# ^ reference local 2
# ^^^^^^ reference local 3
c = helper()
# ^ reference local 4
# ^^^^^^ reference local 5
Expected:helper should reference demo 0.1.0 `demo.lib`/helper(). everywhere, and a/b should reference demo 0.1.0 `demo.gated`/a./b.. An if creates no scope, so these names bind exactly as they do outside the branch. Which branch is "unreachable" depends only on the Python version and platform used for indexing, so the same source gives different indexes on different machines.
Cause:visitName and visitAssignment call evaluator.getDeclarationsForNameNode(node), whose skipUnreachableCode parameter defaults to true. That returns no declarations for unreachable nodes, and emitNameWithoutDeclaration then makes a new local each time. The member-access path in the same file already passes false.
Possible fix (packages/pyright-scip/src/treeVisitor.ts):
With this change, helper, a and b in the example resolve to their real symbols. c, which is only ever assigned inside the unreachable branch, stays one local. The existing snapshot outputs are unchanged. I can open a PR with a snapshot test.
Activity
linear commentedon Oct 21, 2026
OSS-24228