Repository navigation
Describe unfollowed imports and nested-scope variables correctly under includeSymbols - #39
Merged
knutwannheden merged 3 commits intoDec 4, 2026
Conversation
Under includeSymbols, a reference to an import ty cannot follow (`from lib import gone` with `lib` declaring no `gone`, or `import nosuchmod`) falls back to the import's own definition, so it was described as the importing module's symbol: `definedIn: "m"`, `qualifiedName: "m.gone"`. An import declares nothing in the scope it stands in, and a client trusting those fields attributed `gone()` to `m` instead of falling back to the import. Such a symbol now keeps its id and carries only `name`, the shape a file outside any module already gets. A name the scope also binds some other way (a def, class or assignment) is still that scope's symbol. Every definition of the name in its scope is considered, so the description, sent once per session, does not depend on which reference was met first. Plain includeBindings is unchanged: it already gave such a reference no binding.
Describing one symbol per place cannot match each reference when the
place mixes an import ty cannot follow with a real binding. In
from lib import gone
gone()
def f(a=(gone := 1)):
pass
the call reaches only the failed import, yet the walrus binds `gone` at
module scope, so the shared symbol claimed `definedIn: "m"` for it.
The registry key now carries whether the definition is an import. Any
import reaching `symbol_binding` is one `resolve_definition` could not
follow, so that key gets a name-only entry, and references the import
reaches no longer share an id with the place's other bindings. This
replaces the scan over the scope's definitions.
Under includeSymbols, an assignment, loop, `with` or walrus target now
resolves to the definition it creates. Matching by type picked an
earlier binding whose type the scope also holds, which an unfollowed
import's `Unknown` always is. The same pick gave `X = 2` after
`from a import X` the id of `a.X`, and the reads after it `m.X`'s.
Plain includeBindings is unchanged: both changes apply only under
includeSymbols.
ty's `qualified_name_components_from_scope` spells only class and
function scopes. A symbol bound in any other scope read as a module
member under includeSymbols: a lambda parameter `c` as `m.c`, a
comprehension variable `x` as `m.x`, and the type param of
`class C[gone]` as `m.gone`, so a reference to it looked like one to a
module-level `gone`.
Symbol names now spell those scopes, keeping ty's spelling for class and
function scopes:
m.<locals of lambda>.c
m.<locals of list comprehension>.x
m.<type params of class 'C'>.gone
A type-params scope is spelled only for its own type params. It also
encloses the generic class's or function's body, whose names keep their
current spelling (`m.<locals of function 'f'>.a` in `def f[T](a: T)`).
BindingInfo and type descriptors keep ty's spelling. Their bindings are
functions, classes, type aliases and assignments, which only live in
module, class and function scopes.
knutwannheden
deleted the
ty-types-no-defining-module-for-unresolved-imports
branch
December 4, 2026 06:17
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.
Since #38 (released in v1.0.1),
includeSymbolsdescribes some symbols as module members of the file that mentions them. rewrite-python trustsdefinedIn/qualifiedNameto say where a symbol lives, so it declared calls likegone()below on modulem. Eight of itsTestSymbolTheStubsDoNotDeclaretests fail this way.m.pyfrom lib import gone(libdeclares nogone), thengone()m.gone{"name": "gone"}onlylam = lambda c: cm.cm.<locals of lambda>.c[x for x in xs]m.xm.<locals of list comprehension>.xclass C[T]: ...m.Tm.<type params of class 'C'>.Tfrom a import X, then the target ofX = 2a.X's idm.X's id, shared with the reads after itThere are two causes:
qualified_name_components_from_scopespells only class and function scopes, so a variable in a lambda, a comprehension or a type-params scope reads as the module's.An unfollowed import is a symbol of its own
The symbol registry key now carries whether the definition is an import.
resolve_definitionfollows every import it can, so any import reachingsymbol_bindingis one it could not follow, and its entry carriesnamealone. Other bindings of the name keep the scope's symbol:A symbol is described once per session, so one entry per place cannot match each reference when a failed import and a real binding share a name. Keeping
definedInwhenever any binding declares the name mislabels the call above. Dropping it whenever any binding is a failed import stripsm.gonefrom the walrus's reads. The cost of splitting is two ids for a variable bound both ways, which is rare.The split needed one more fix. Under
includeSymbols, an assignment, loop,withor walrus target now resolves to the definition it creates.binding_definitionpicks the first candidate whose type the scope holds, and an unfollowed import'sUnknownalways qualifies. So the walrus target got the import's id. The same pick gave theX = 2target in the table the id ofa.X.Scope spelling
Symbol names spell lambda and comprehension scopes wherever they occur. A type-params scope is spelled only for its own type params, since it also encloses the generic function's body, whose locals keep
m.<locals of function 'f'>.a.BindingInfoand type descriptors keep ty's spelling: their bindings are defs, classes, type aliases and assignments, which only live in module, class and function scopes.Compatibility and tests
Plain
includeBindingsoutput is unchanged, since both resolution changes are gated onincludeSymbols. No field is added or removed. Only the values above change.Three integration tests cover failed imports getting a name-only symbol, the walrus case getting separate ids, and each scope's spelling, including a generic function's local. Each was mutation-checked: dropping the key's import flag, the target preference, or the type-params guard fails one of them.
Not changed here: in
try: from fastlib import f/except ImportError: from slowlib import fwithfastlibmissing,f()still resolves to the failed import in both modes, whereslowlib.fis right. Fixing that changes plainincludeBindingsoutput too.