[ty] Fix parameter references across files via keyword args#23012
[ty] Fix parameter references across files via keyword args#23012BurntSushi merged 4 commits intoastral-sh:mainfrom
Conversation
|
@Gankra or @BurntSushi , is one of you able to review this? |
BurntSushi
left a comment
There was a problem hiding this comment.
Very nice work! Lovely tests.
crates/ty_ide/src/references.rs
Outdated
| start = after; | ||
| continue; | ||
| } | ||
| } |
There was a problem hiding this comment.
Is this word boundary check actually needed? If you omit it, then you'll next skip over any whitespace and do a check for =. This implicitly requires a word boundary.
There was a problem hiding this comment.
Thanks for pointing this out, it was over-zealous, I removed the post-name boundary check and now rely on the existing whitespace + single = check, it reads better and works the same
d85bf9b to
4027b37
Compare
crates/ty_ide/src/references.rs
Outdated
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
There's still a bunch of code duplication here. Pretty much all of check_identifier_reference and check_reference_from_covering_node are being duplicated.
What I was thinking was having both KeywordArgumentReferencesFinder and LocalReferencesFinder share a common representation. You still need both types for the AST visiting, but it seems like they could both sit on top of a shared core.
I might be wrong about this because I haven't tried it. So if it's too unnatural, I think just adding a comment noting why there is code duplication here would be sufficient.
There was a problem hiding this comment.
Thanks for catching that, I pushed a follow-up that de-dupes the shared logic
Though I kept LocalReferencesFinder and KeywordArgumentReferencesFinder as separate visitor types, since they still differ in how they classify/emits references, but the matching/resolution path is now shared
Or would you prefer a deeper shared-core type?
There was a problem hiding this comment.
The only difference I can see between the two of them is that we're using ReferenceKind::Other explicitly in one and determine_reference_kind in the other. But it seems like we could just use determine_reference_kind in both. And then the differences disappear.
Moreover, you have:
struct KeywordArgumentReferencesFinder<'a> {
model: &'a SemanticModel<'a>,
tokens: &'a Tokens,
target_definitions: &'a NavigationTargets,
references: &'a mut Vec<ReferenceTarget>,
mode: ReferencesMode,
target_text: &'a str,
ancestors: Vec<AnyNodeRef<'a>>,
}But we already have:
struct LocalReferencesFinder<'a> {
model: &'a SemanticModel<'a>,
tokens: &'a Tokens,
target_definitions: &'a NavigationTargets,
references: &'a mut Vec<ReferenceTarget>,
mode: ReferencesMode,
target_text: &'a str,
ancestors: Vec<AnyNodeRef<'a>>,
}They are identical. I think we can de-duplicate more here. e.g.,
struct KeywordArgumentReferencesFinder<'a>(LocalReferencesFinder<'a>);Or something similar.
There was a problem hiding this comment.
Thanks for the tip with the tuple wrapper, I've implemented it as-is!
91435dd to
0776e1e
Compare
BurntSushi
left a comment
There was a problem hiding this comment.
OK, this LGTM now! Thanks!
* main: (45 commits) [ty] Fix wrong inlay hints for overloaded function arguments (astral-sh#23179) [ty] Respect `@no_type_check` when combined with other decorators (astral-sh#23177) [ty] Use type context when inferring constructor argument types (astral-sh#23139) [`airflow`] Add ruff rules to catch deprecated attribute access from context key for Airflow 3.0 (`AIR301`) (astral-sh#22850) Support formatting `pycon` markdown code blocks (astral-sh#23112) Markdown formatting in LSP (astral-sh#23063) Instruct Claude to use comments more sparingly (astral-sh#23181) [`flake8-gettext`] Fix false negatives for plural argument of ngettext (`INT001`, `INT002`, `INT003`) (astral-sh#21078) [ty] Invoking goto-def on parentheses of a class constructor call takes you too constructor method [ty] Make goto definition on class constructor always go to class definition [ty] Assign lower completions ranking to deprecated functions and classes (astral-sh#23089) [ty] Fix parameter references across files via keyword args (astral-sh#23012) [ty] Exclude enclosing class for base completions (astral-sh#23141) [`pyupgrade`] Fix syntax error on string with newline escape and comment (`UP037`) (astral-sh#22968) [ty] Improve documentation for `expect_single_definition` method (astral-sh#23175) [ty] Configure check mode for all projects Add `home-assistant` to ecosystem projects (astral-sh#23132) Add tabbed shell completion documentation (astral-sh#23169) Bump typing conformance-suite pin (astral-sh#23174) [ty] Fix invalid diagnostic location for a sub-call to a specialized ParamSpec (astral-sh#23036) ...
Summary
Fixes astral-sh/ty#2362
Find references/renamefrom parameter definitions to include cross-file keyword-argument labels, using semantic validation (no text-only matches)name=(but not==) candidate filterTest Plan
__init__, top-level function, andasync defparameters