Skip to content

[ty] Update __slots__ string when renaming an attribute#26438

Open
SuryanshSS1011 wants to merge 1 commit into
astral-sh:mainfrom
SuryanshSS1011:ty-rename-slots
Open

[ty] Update __slots__ string when renaming an attribute#26438
SuryanshSS1011 wants to merge 1 commit into
astral-sh:mainfrom
SuryanshSS1011:ty-rename-slots

Conversation

@SuryanshSS1011

Copy link
Copy Markdown

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, renaming self.value on one of them leaves the other one alone.

Scope

This is intentionally a first cut, matching what was discussed on the issue:

  • same class only (no subclass/superclass propagation)
  • __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)
  • single-part string literals only (implicitly concatenated strings like "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_ide covering 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, and cargo fmt all pass.

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.
@SuryanshSS1011 SuryanshSS1011 requested a review from a team as a code owner June 28, 2026 04:46
@astral-sh-bot astral-sh-bot Bot added server Related to the LSP server ty Multi-file analysis & type inference labels Jun 28, 2026
@astral-sh-bot astral-sh-bot Bot requested a review from lerebear June 28, 2026 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Rename symbol" does not change string slot listed in __slots__ even if it is a simple assignment

1 participant