Skip to content

Python: give name and parameter Variables the owner that binds them - #10318

Merged
knutwannheden merged 3 commits into
mainfrom
python-give-name-variables-an-owner
Dec 4, 2026
Merged

knutwannheden merged 3 commits into
mainfrom
python-give-name-variables-an-owner

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

In rewrite-python, a JavaType.Variable for a name or parameter has owner None. A recipe therefore cannot tell which function a local belongs to, or a module-level variable from a local. Java's type mapping gives a parameter or local the enclosing method's JavaType.Method, and a field its class. #10313 left this out deliberately because it changes what recipes see.

With this change every such Variable gets the owner that binds it:

g = 1                         # g: the module class `m`, also where another file imports g
def outer(p):                 # p: outer's Method
    a = p                     # a: outer's Method, also inside inner through nonlocal
    def inner():
        nonlocal a
        return a + g
    f = lambda q: q + a       # q: outer's Method
    return [y for y in (p,)]  # y: outer's Method
class C:
    x = 1                     # x, y: the class C
    y = x

The owner is the same object as the method declaration's method_type or the class declaration's type. Where ty gives no binding, the owner stays None.

The binding scope comes from ty's symbol

Since #10313 ty-types names each binding's symbol, and its qualifiedName spells the scope that binds it, as in m.<locals of function 'outer'>.a. It follows nonlocal, global and class-body scoping, all of which "the function the parser is inside" gets wrong.

The path alone cannot pick the function: a property's getter and setter both spell <locals of function 'value'>. So it is matched against the scopes enclosing the reference in the file's ast, one of which binds the name. Decorators, defaults and annotations count in the scope around their function. A path that does not match leaves the owner None.

A lambda or comprehension has no JavaType, so its variables belong to the scope around it, as Java's enclosing method owns a lambda's parameters. The path is matched only up to that scope. This also absorbs ty-types 1.0.2 placing an assignment expression's target in its comprehension (fixed in openrewrite/ty-types#40), which would otherwise give the target and a later read different owners.

One owner object per declaration

_shared_variable keys a Variable on id(owner), so a parameter, its uses and the declaration have to reach the same owner object. method_declaration_type built a new Method on every call. It now keeps one per function, and a class owner is resolved once per class.

Tests

test_type_interning.py adds tests for a function's variables (a nested nonlocal, a lambda parameter, a comprehension variable), for module and class-body variables including an imported module variable, and for an assignment expression read after its comprehension. The getter/setter sharing test now also checks that each function owns its own locals. Removing the Method cache fails the function and getter/setter tests and the existing parameter-sharing test.

Not changed here

  • A property getter with only self and no return annotation has no method_type, so its locals keep owner None.
  • References inside a PEP 695 type-parameter scope get no owner.

A JavaType.Variable for a name or parameter now has an owner, as Java's
type mapping gives one: the JavaType.Method of the function whose scope
binds it, the class for a class-body name, and the module's class for a
module-level name, also where another file imports it. A lambda or a
comprehension has no type, so the scope around it owns its variables, as
Java's enclosing method owns a lambda's parameters.

The binding scope comes from ty's symbol: its qualifiedName follows
nonlocal, global and class-body scoping. That path cannot tell two
functions of one name apart, such as a property's getter and setter, so
it is matched against the scopes enclosing the reference in the file's
ast, one of which binds the name. A path that does not match leaves the
owner None.

method_declaration_type keeps one Method per function, so a parameter,
its uses and the method declaration reach the same owner object and the
Variable sharing keyed on the owner holds.
…ts comprehension

ty-types places an assignment expression's target in its comprehension's
scope, so a read of the variable after the comprehension did not match
the path and got no owner, while the target got the enclosing function.
The two then split into two Variables. A lambda or comprehension never
owns a variable, so the path is now matched only up to the scope that
does.
A class body's names each asked declared_class_type for their owner,
which resolves the class's decorators and bases again every time.
@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 1daa119 into main Dec 4, 2026
1 check passed
@knutwannheden
knutwannheden deleted the python-give-name-variables-an-owner branch December 4, 2026 08:44
@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