Skip to content

Comments

[ty] Fix parameter references across files via keyword args#23012

Merged
BurntSushi merged 4 commits intoastral-sh:mainfrom
Hugo-Polloli:find-param-references
Feb 9, 2026
Merged

[ty] Fix parameter references across files via keyword args#23012
BurntSushi merged 4 commits intoastral-sh:mainfrom
Hugo-Polloli:find-param-references

Conversation

@Hugo-Polloli
Copy link
Contributor

@Hugo-Polloli Hugo-Polloli commented Feb 1, 2026

Summary

Fixes astral-sh/ty#2362

  • Fix Find references/rename from parameter definitions to include cross-file keyword-argument labels, using semantic validation (no text-only matches)
  • Keep perf predictable via an owner-visibility gate (top-level funcs / methods only) + a cheap name= (but not ==) candidate filter
  • Match against all declaration targets to avoid ordering-sensitive misses which surfaced additional correct targets for overloads/imports in tests (should be more correct

Test Plan

  • Added multi-file coverage for __init__, top-level function, and async def parameters
  • Added negatives (attribute vs parameter; nested function; same-name parameters across methods)
  • Added rename coverage to ensure keyword labels update across files
  • Updated snapshots for multi-declaration cases (e.g. overloads, submodule imports) that become more correct with the new "match all declaration targets"

@AlexWaygood AlexWaygood added server Related to the LSP server ty Multi-file analysis & type inference labels Feb 1, 2026
@Hugo-Polloli Hugo-Polloli marked this pull request as ready for review February 1, 2026 21:35
@carljm
Copy link
Contributor

carljm commented Feb 3, 2026

@Gankra or @BurntSushi , is one of you able to review this?

@carljm carljm requested review from BurntSushi and removed request for carljm February 3, 2026 19:40
@sharkdp sharkdp removed their request for review February 4, 2026 08:07
Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Very nice work! Lovely tests.

start = after;
continue;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip with the tuple wrapper, I've implemented it as-is!

Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

OK, this LGTM now! Thanks!

@BurntSushi BurntSushi merged commit ac25c6b into astral-sh:main Feb 9, 2026
46 checks passed
carljm added a commit to Hugo-Polloli/ruff that referenced this pull request Feb 9, 2026
* 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)
  ...
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.

Find all references in VS Code extension does not find call sites

4 participants