Repository navigation
Python: give name and parameter Variables the owner that binds them - #10318
Merged
Merged
Conversation
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.
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.
In rewrite-python, a
JavaType.Variablefor a name or parameter hasownerNone. 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'sJavaType.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:
The owner is the same object as the method declaration's
method_typeor the class declaration'stype. 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
qualifiedNamespells the scope that binds it, as inm.<locals of function 'outer'>.a. It followsnonlocal,globaland 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'sast, 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_variablekeys a Variable onid(owner), so a parameter, its uses and the declaration have to reach the same owner object.method_declaration_typebuilt 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.pyadds tests for a function's variables (a nestednonlocal, 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
selfand no return annotation has nomethod_type, so its locals keep owner None.