Skip to content

Describe unfollowed imports and nested-scope variables correctly under includeSymbols - #39

Merged
knutwannheden merged 3 commits into
mainfrom
ty-types-no-defining-module-for-unresolved-imports
Dec 4, 2026
Merged

knutwannheden merged 3 commits into
mainfrom
ty-types-no-defining-module-for-unresolved-imports

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

Since #38 (released in v1.0.1), includeSymbols describes some symbols as module members of the file that mentions them. rewrite-python trusts definedIn/qualifiedName to say where a symbol lives, so it declared calls like gone() below on module m. Eight of its TestSymbolTheStubsDoNotDeclare tests fail this way.

m.py symbol before symbol after
from lib import gone (lib declares no gone), then gone() m.gone {"name": "gone"} only
lam = lambda c: c m.c m.<locals of lambda>.c
[x for x in xs] m.x m.<locals of list comprehension>.x
class C[T]: ... m.T m.<type params of class 'C'>.T
from a import X, then the target of X = 2 a.X's id m.X's id, shared with the reads after it

There are two causes:

  • Failed imports. A reference to an import ty cannot follow falls back to the import's own definition, which Report bindings as session-stable symbol ids under includeSymbols #38 keyed with the place's other bindings and described as the importing module's.
  • Unspelled scopes. ty's qualified_name_components_from_scope spells 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_definition follows every import it can, so any import reaching symbol_binding is one it could not follow, and its entry carries name alone. Other bindings of the name keep the scope's symbol:

from lib import gone
gone()                    # the import's id: {"name": "gone"}
def f(a=(gone := 1)):     # the walrus binds `gone` at module scope: m.gone
    pass
gone                      # m.gone

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 definedIn whenever any binding declares the name mislabels the call above. Dropping it whenever any binding is a failed import strips m.gone from 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, with or walrus target now resolves to the definition it creates.

binding_definition picks the first candidate whose type the scope holds, and an unfollowed import's Unknown always qualifies. So the walrus target got the import's id. The same pick gave the X = 2 target in the table the id of a.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. BindingInfo and 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 includeBindings output is unchanged, since both resolution changes are gated on includeSymbols. 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 f with fastlib missing, f() still resolves to the failed import in both modes, where slowlib.f is right. Fixing that changes plain includeBindings output too.

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 knutwannheden self-assigned this Dec 4, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Dec 4, 2026
@knutwannheden
knutwannheden merged commit 899f194 into main Dec 4, 2026
7 checks passed
@knutwannheden
knutwannheden deleted the ty-types-no-defining-module-for-unresolved-imports branch December 4, 2026 06:17
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant