Skip to content

Report bindings as session-stable symbol ids under includeSymbols - #38

Merged
knutwannheden merged 1 commit into
mainfrom
ty-types-symbol-ids-for-bindings-includesymbols
Dec 4, 2026
Merged

knutwannheden merged 1 commit into
mainfrom
ty-types-symbol-ids-for-bindings-includesymbols

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

rewrite-python builds a JavaType.Variable for each name reference ty reports, and keeps every type it receives for the rest of a project parse. In a file of 200 lines of values.append(str(i).zfill(4)) that is 201 Variables for the one variable values. Sharing one per variable needs a key that is equal for two references to the same variable and different for two different variables. binding can't serve as that key:

  • Parameters, loop, with and except targets, walrus, comprehension and match variables get no binding, because Definition::name is None for those kinds.
  • qualifiedName spells function scopes for display, so a property getter's local r and its setter's local r both read m.C.<locals of function 'value'>.r.

A symbol id per variable

The new getTypes flag includeSymbols implies includeBindings and replaces each binding with an id. The symbols table describes each id the first time a session sees it, as types does for descriptors. For def f(a): return a + 1, an excerpt of the result:

"nodes": [
  {"nodeKind": "Parameter", "start": 6, "end": 7, "binding": {"symbolId": 7}},
  {"nodeKind": "ExprName", "start": 17, "end": 18, "binding": {"symbolId": 7}}
],
"symbols": {"7": {"name": "a", "definedIn": "m", "qualifiedName": "m.<locals of function 'f'>.a"}}

The id is keyed on the (file, scope, place) of the definition the reference resolves to. Every assignment to x in one scope therefore shares an id, and same-named locals of two functions don't. The salsa id of the Definition was rejected because it identifies one assignment and its docs say it can change between runs. Names come from ty's place table, which covers every definition kind. A file that resolves to no module, such as my-script.py, keeps its ids, and its symbols entries leave out definedIn and qualifiedName.

The ruff fork needs no change. Definition::file_scope and Definition::place are already public in ty_python_core.

Compatibility and payload

The flag defaults off, and an older build ignores it. symbols is present, possibly as {}, exactly when the flag is honored, so a client can tell whether the build supports it. Over the 71 files of abseil-py, output with no flag and with includeBindings parses equal to main on every response. includeSymbols reports 75% more bindings for slightly less payload:

bytes over no bindings bindings
no bindings 12,271,371
includeBindings 14,760,009 +20.3% 20,706
includeSymbols 14,575,087 +18.8% 36,301

Tests

The new integration tests cover these cases:

  • a parameter's declaration and its uses share one id, lambdas included
  • every local binding kind gets an id
  • getter and setter locals get different ids
  • a closure read gets the enclosing function's id
  • an imported name gets the defining module's id, and is described once per session
  • in a class body, x = x reads the module's x and binds the class's
  • global and nonlocal names get the owning scope's id
  • a hyphenated script keeps its ids
  • the one-shot --bindings --symbols combination reports symbol ids

Not changed here

  • self.x assigned only in a method has no binding. Where the class body binds x, self.x carries the class variable's id.
  • qualifiedName keeps the display helper's spelling. A type parameter T of def f[T] reads m.T. Clients key a variable by its id.
  • getTypeRegistry doesn't return symbols.

A new getTypes flag, includeSymbols, implies includeBindings and reports
each binding as {"symbolId": N}. The response carries a `symbols` table
describing each id (name, definedIn, qualifiedName) once per session, as
`types` does for descriptors.

An id stands for a variable: the (file, scope, place) of the definition
a reference resolves to, so every assignment to `x` in one scope shares
it. The salsa id of a Definition is per assignment and may change
between revisions. Names come from the place table, so parameters,
loop/with/except targets, walrus, comprehension and match variables
resolve too, and parameter declarations carry the id of their uses.

Without the flag, output is unchanged. The one-shot CLI gains --symbols.
@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 9495d7e into main Dec 4, 2026
7 checks passed
@knutwannheden
knutwannheden deleted the ty-types-symbol-ids-for-bindings-includesymbols branch December 4, 2026 05:07
@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