[ty] Support renaming import aliases#21792
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
CodSpeed Performance ReportMerging #21792 will not alter performanceComparing Summary
Footnotes
|
89e5f3e to
86b8077
Compare
8d919bb to
56408e3
Compare
| DocumentHighlights, | ||
| } | ||
|
|
||
| impl ReferencesMode { |
There was a problem hiding this comment.
We might want to unify ReferencesMode and ImportAliasResolution because I think there might be more differences in resolve_* moving forward where it might even be beneficial to know: Hey, what request are we handling, it's rename, cool, let's do this instead of x.
Gankra
left a comment
There was a problem hiding this comment.
Hmm is this rebased on main? I think some from..import tests I added should also have had snapshot updates.
| // We prefer the specific overload for hover, go-to-def etc. However, | ||
| // `definitions_for_callable` always resolves import aliases. That's why we | ||
| // skip it in cases import alias resolution is turned of (rename, highlight references). | ||
| if alias_resolution == ImportAliasResolution::ResolveAliases { | ||
| definitions.extend(definitions_for_callable(model, call)); | ||
| } |
There was a problem hiding this comment.
Concretely: the reason GotoTarget::Call exists if for when you click on Class(). definitions_for_expression will find class Class while definitions_for_callable will find def __init__(). So indeed we want to excludedefinitions_for_callable when we don't want to ResolveAliases!
There was a problem hiding this comment.
Ohh, thank you! That makes sense
| .source( | ||
| "main.py", | ||
| r#" | ||
| from lib import deprecated<CURSOR> |
There was a problem hiding this comment.
the absolute confidence of a ruff dev to be like "yeah of course that cursor is on deprecated"
There was a problem hiding this comment.
I debugged way too many TextRange problems :)
I rebased a couple of times today. Any specific examples? I don't see any other I did see some changes to your submodule import tests when trying to fix overload resolution that looked related to redeclarations. |
56408e3 to
adf468b
Compare
|
I'll go ahead and merge this. Happy to address any tests that should have been updated but didn't in a follow up PR |
|
Ah good point, the cases I got in are specifically failing on non-aliases, so no worries. |
* origin/main: [ty] Allow `tuple[Any, ...]` to assign to `tuple[int, *tuple[int, ...]]` (#21803) [ty] Support renaming import aliases (#21792) [ty] Add redeclaration LSP tests (#21812) [ty] more detailed description of "Size limit on unions of literals" in mdtest (#21804) [ty] Complete support for `ParamSpec` (#21445) [ty] Update benchmark dependencies (#21815)
* origin/main: [ty] Add test case for fixed panic (#21832) [ty] Avoid double-analyzing tuple in `Final` subscript (#21828) [flake8-bandit] Fix false positive when using non-standard `CSafeLoader` path (S506). (#21830) Add minimal-size build profile (#21826) [ty] Allow `tuple[Any, ...]` to assign to `tuple[int, *tuple[int, ...]]` (#21803) [ty] Support renaming import aliases (#21792) [ty] Add redeclaration LSP tests (#21812) [ty] more detailed description of "Size limit on unions of literals" in mdtest (#21804) [ty] Complete support for `ParamSpec` (#21445) [ty] Update benchmark dependencies (#21815)
Summary
Fixes renaming import aliases or symbols that include an import alias in their chain.
For renames (and highlight references), we mustn't resolve to the final declaration. Instead, we should only resolve to the first declaration that defined this specific name (e.g. the import alias). We already made this distinction internally but we didn't correctly propagate the
alias_resolution, which resulted in some definitions resolving to the final declarations and others only resolved to the first import alias.It's fairly likely that we still fail to propagate
ImportAliasResolutionin some places but we can fix those as they come up.This PR fixes this.
I also decided to split
GotoTarget::ImportSymbolAliasinto two variants, one for when the target is the alias'sasnameand one where it's the alias's name. Mainly because I found the distinction only based onrangehard to follow and this also revealed some further bugs.Fixes astral-sh/ty#1661
Test plan
Screen.Recording.2025-12-05.at.11.54.26.mov