Skip to content

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

Merged
lerebear merged 5 commits into
astral-sh:mainfrom
SuryanshSS1011:ty-rename-slots
Jul 6, 2026
Merged

lerebear merged 5 commits into
astral-sh:mainfrom
SuryanshSS1011:ty-rename-slots

Conversation

@SuryanshSS1011

Copy link
Copy Markdown
Contributor

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.

@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 Ruff or ty LSP server ty The ty type checker labels Jun 28, 2026
@astral-sh-bot
astral-sh-bot Bot requested a review from lerebear June 28, 2026 04:46

@lerebear lerebear left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution! It looks good in large part, but I've commented on a pair of bugs that should be addressed before this ships.

Comment thread crates/ty_ide/src/references.rs Resolved
@SuryanshSS1011

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review @lerebear.

I have made the the fix in target_belongs_to_class:

  1. Nested class renaming an outer slot: The check now finds the nearest lexically-enclosing class of the target and requires it to be the class that declares the __slots__, so Inner.value no longer touches Outer.__slots__.
  2. Non-attribute definitions renaming the slot: It now accepts a definition only if it's a member (self.value = ...) or a bare class-body annotation (value: int with no value), so a parameter or local sharing the name is left alone.

One thing that came up while validating the pseudocode is that ancestor_scopes skips class scopes during name resolution, so it can't be used to find the nearest enclosing class. I switched to walking the lexical parents via Scope::parent() instead, which gives the innermost class directly.

I added the class-body-annotation test you suggested, plus a regression test for each of the two bugs (rename_parameter_does_not_touch_slots and rename_nested_class_attribute_does_not_touch_outer_slots). Full ty_ide suite passes.

@SuryanshSS1011
SuryanshSS1011 requested a review from lerebear July 2, 2026 20:37
SuryanshSS1011 and others added 5 commits July 5, 2026 20:44
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.

@lerebear lerebear left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the edits: the latest looks good. I made a few minor modifications in preparation for this to merge.

@lerebear
lerebear enabled auto-merge (squash) July 6, 2026 03:51
@lerebear
lerebear merged commit f1da30d into astral-sh:main Jul 6, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the Ruff or ty LSP server ty The ty type checker

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

2 participants