[ty] Update __slots__ string when renaming an attribute#26438
Open
SuryanshSS1011 wants to merge 1 commit into
Open
[ty] Update __slots__ string when renaming an attribute#26438SuryanshSS1011 wants to merge 1 commit into
__slots__ string when renaming an attribute#26438SuryanshSS1011 wants to merge 1 commit into
Conversation
Renaming an instance attribute now also updates the matching string in the enclosing class's `__slots__`, the way Pylance does. When the references visitor hits a string literal, it checks whether the string sits in a `__slots__` assignment in a class body (tuple, list, set, or dict keys) and whether the attribute being renamed belongs to that same class. If so, it emits an edit for the inner content of the string, leaving the quotes in place. The same-class check walks the ancestor scopes of the target's definitions, so an unrelated class with a slot of the same name is left alone. Scope for now: same class only, dict keys only, single-part strings.
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.
Fixes astral-sh/ty#3674.
When you rename an instance attribute, ty now also renames the matching string in the class's
__slots__. Pylance does this and the issue asks for the same behavior.How it works
The references visitor already walks every node looking for matches. I added a check on string literals: when it hits one, it looks at whether the string is sitting in a
__slots__assignment inside a class body, and whether the attribute being renamed actually belongs to that class. If both hold, it emits an edit for the inner content of the string (the quotes stay put).The same-class check walks the ancestor scopes of the rename target's definitions and looks for the class that owns the
__slots__. So if two unrelated classes both have a"value"slot, renamingself.valueon one of them leaves the other one alone.Scope
This is intentionally a first cut, matching what was discussed on the issue:
__slots__written as a tuple, list, set, or dict literal; for a dict, only the keys are renamed (values are left as-is, which is what was requested)"a" "b"are skipped)Renaming is still driven from the attribute itself. Starting a rename on the
__slots__string directly isn't supported, since a string literal isn't a renameable symbol on its own.Tests
Added rename tests in
ty_idecovering tuple/list/set/dict-key slots, the same-class isolation case, that dict values are not touched, and that starting from the slot string is rejected.cargo test -p ty_ide,cargo clippy -p ty_ide, andcargo fmtall pass.