Repository navigation
Fix static conditional evaluation for shadowed sys/os names - #12035
Closed
刘亦达 (zhengdaodeguL) wants to merge 1 commit into
Closed
刘亦达 (zhengdaodeguL) wants to merge 1 commit into
刘亦达 (zhengdaodeguL) wants to merge 1 commit into
Conversation
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
Contributor
Member
|
Local review: All four focused tests passed. The |
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #12033
Summary
Static conditional evaluation treated a parameter named
sysorosas the module itself.staticExpressions.tsmatchedsys/osby spelling only, using the file-level import-alias list collected by the binder without checking scope:_isSysVersionInfoExpression/_isSysPlatformInfoExpressionmatched against file-levelsysImportAliases, and_isOsNameInfoExpressiononly recognized the spellingos(no alias list at all). The binder therefore folded branches as unreachable even when a parameter shadowed the module, andos.namewas folded even when the file never importedos.The fix filters the file-level alias list by the current scope: an alias is kept only when
lookUpSymbolRecursiveresolves all of its declarations toDeclarationType.Aliaswith a matchingmoduleName. A parameter shadowing the module (aParamdeclaration) filters the alias out, so the branch is no longer statically evaluated; with noimport os, the list is empty and nothing folds.osimport aliases are now collected and threaded through the static-expression evaluator the same waysysaliases already were.Evidence and validation
Baseline and head revisions: head
da99088c0(parentff00b031c).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, samplesstaticExpression3.py/staticExpression4.py);StaticExpression*;checker.test;typeEvaluator1-8.test;pnpm run build;tsc --noEmit.Observed results:
staticExpression3.pyreportsExpected 8 infos, got 0, i.e. all four shadowed-parameter branches were statically folded and theirreveal_typecalls suppressed.StaticExpressionShadowedModules1 passed;StaticExpression*4 passed;checker.test78 passed;typeEvaluator1-8.test8 suites / 1,461 tests passed;pnpm run build(tsc) passed;tsc --noEmitpassed; Prettier andgit diff --checkpassed.StaticExpression1/StaticExpression2samples still pass, and aliased imports such asimport sys as _sysstill resolve through the scope filter.Limitations or unrun checks: the full
pnpm testsuite 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/osmodule 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.