Skip to content

Fix static conditional evaluation for shadowed sys/os names - #12035

Closed
刘亦达 (zhengdaodeguL) wants to merge 1 commit into
microsoft:mainfrom
zhengdaodeguL:fix/static-conditional-sys-os-param-12033
Closed

刘亦达 (zhengdaodeguL) wants to merge 1 commit into
microsoft:mainfrom
zhengdaodeguL:fix/static-conditional-sys-os-param-12033

Conversation

@zhengdaodeguL

Copy link
Copy Markdown

Fixes #12033

Summary

Static conditional evaluation treated a parameter named sys or os as the module itself. staticExpressions.ts matched sys/os by spelling only, using the file-level import-alias list collected by the binder without checking scope: _isSysVersionInfoExpression / _isSysPlatformInfoExpression matched against file-level sysImportAliases, and _isOsNameInfoExpression only recognized the spelling os (no alias list at all). The binder therefore folded branches as unreachable even when a parameter shadowed the module, and os.name was folded even when the file never imported os.

The fix filters the file-level alias list by the current scope: an alias is kept only when lookUpSymbolRecursive resolves all of its declarations to DeclarationType.Alias with a matching moduleName. A parameter shadowing the module (a Param declaration) filters the alias out, so the branch is no longer statically evaluated; with no import os, the list is empty and nothing folds. os import aliases are now collected and threaded through the static-expression evaluator the same way sys aliases already were.

Evidence and validation

  • Added or updated focused regression coverage for the motivating behavior.
  • Checked a nearby invalid case that must remain rejected and an unaffected neighboring control.
  • Compared focused diagnostics and revealed types against the pre-change baseline where feasible.
  • Reviewed the complete patch for architectural fit, type-precision changes, and performance impact.
  • Ran the relevant tests and checks listed below.

Baseline and head revisions: head da99088c0 (parent ff00b031c).

Commands and configuration: npm run test -- typeEvaluator8 (and typeEvaluator1-7), checker.test, pnpm run build, tsc --noEmit, Prettier, git diff --check.

Tests and checks run: StaticExpressionShadowedModules (new test, samples staticExpression3.py / staticExpression4.py); StaticExpression*; checker.test; typeEvaluator1-8.test; pnpm run build; tsc --noEmit.

Observed results:

  • Pre-change baseline: the new test fails — staticExpression3.py reports Expected 8 infos, got 0, i.e. all four shadowed-parameter branches were statically folded and their reveal_type calls suppressed.
  • Head: StaticExpressionShadowedModules 1 passed; StaticExpression* 4 passed; checker.test 78 passed; typeEvaluator1-8.test 8 suites / 1,461 tests passed; pnpm run build (tsc) passed; tsc --noEmit passed; Prettier and git diff --check passed.
  • Neighboring control: existing StaticExpression1/StaticExpression2 samples still pass, and aliased imports such as import sys as _sys still resolve through the scope filter.

Limitations or unrun checks: the full pnpm test suite was not run (only typeEvaluator1-8 + checker + build + tsc); CI (Windows/macOS runners) has not run yet.

Type precision and performance

No type widening: the change only stops static folding when the name does not actually refer to the sys/os module in the current scope. Previously-folded branches are now analyzed normally, which is the correct behavior. The scope filter runs once per static-expression evaluation over the file-level alias list (a handful of entries), so there is no material performance cost.

Static evaluation of sys.version_info, sys.platform, and os.name treated
any name spelled sys/os as the imported module, even when a parameter (or
other local) shadowed the import, and os.name was folded even when os was
never imported. The binder now filters the file-wide import-alias lists
through the current scope: an alias survives only when it resolves to an
import alias declaration for that module. os import aliases are now
collected and threaded into the static-expression evaluator alongside the
existing sys aliases.

Fixes microsoft#12033
@crypt0lith

Copy link
Copy Markdown
Contributor

Thanks for the PR, but I opened #12033 to track the bug, and the fix is already in #11929.

@bschnurr

Copy link
Copy Markdown
Member

Local review: da99088c integrated with ff00b031.

All four focused tests passed. The if statement fix also restored two argument errors that main hid when parameters shadow sys and os. One related path is still affected on both revisions: with class Fake: name: str = "nt", def f(os: Fake): return 1 if os.name == "nt" else "other" is inferred as returning only Literal["other"] on a Linux target, but f(Fake()) returns 1 on CPython. Can the ternary evaluator use the same scope-aware lookup, or should we track that separately?

@rchiodo

Copy link
Copy Markdown
Collaborator

Thanks for working on this! We're closing this in favor of #11929, which also fixes #12033. That PR resolves sys/os/typing names by looking them up in the current scope instead of the per-file import-alias lists, which addresses the root cause and also fixes #11927 and #12034.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Static conditional evaluation treats a parameter named sys or os as the module

4 participants