Skip to content

Comments

Implement goto-definition and find-references for global/nonlocal statements#21616

Merged
Gankra merged 1 commit intomainfrom
gankra/non-exprs
Nov 25, 2025
Merged

Implement goto-definition and find-references for global/nonlocal statements#21616
Gankra merged 1 commit intomainfrom
gankra/non-exprs

Conversation

@Gankra
Copy link
Contributor

@Gankra Gankra commented Nov 24, 2025

Summary

The implementation here is to just record the idents of these statements in scopes_by_expression (which already supported idents but only ones that happened to appear in expressions), so that definitions_for_name Just Works.

goto-type (and therefore hover) notably does not work on these statements because the typechecker does not record info for them. I am tempted to just introduce type_for_name which runs definitions_for_name to find other expressions and queries the inferred type... but that's a bit whack because it won't be the computed type at the right point in the code. It probably wouldn't be particularly expensive to just compute/record the type at those nodes, as if they were a load, because global/nonlocal is so scarce?

Test Plan

Snapshot tests added/re-enabled.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 24, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 24, 2025

mypy_primer results

No ecosystem changes detected ✅

No memory usage changes detected ✅

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Hmm, the changes in this PR seem to be primarily about string annotations and not nonlocal and global?

@Gankra
Copy link
Contributor Author

Gankra commented Nov 25, 2025

Yes it's based on top of my string attr PR which I have rebased without rebasing this one 😓

Base automatically changed from gankra/strattr to main November 25, 2025 13:31
@Gankra Gankra marked this pull request as ready for review November 25, 2025 13:36
@Gankra
Copy link
Contributor Author

Gankra commented Nov 25, 2025

It is now rebased on main and ready to review.

@MichaReiser MichaReiser added server Related to the LSP server ty Multi-file analysis & type inference labels Nov 25, 2025
@Gankra Gankra merged commit 209ea06 into main Nov 25, 2025
43 checks passed
@Gankra Gankra deleted the gankra/non-exprs branch November 25, 2025 13:56
carljm added a commit to mtshiba/ruff that referenced this pull request Nov 25, 2025
* main:
  [ty] Implement `typing.override` (astral-sh#21627)
  [ty] Avoid expression reinference for diagnostics (astral-sh#21267)
  [ty] Improve autocomplete suppressions of keywords in variable bindings
  [ty] Only suggest completions based on text before the cursor
  Implement goto-definition and find-references for global/nonlocal statements (astral-sh#21616)
  [ty] Inlay Hint edit follow up (astral-sh#21621)
  [ty] Implement lsp support for string annotations (astral-sh#21577)
  [ty] Add 'remove unused ignore comment' code action (astral-sh#21582)
  [ty] Refactor `CheckSuppressionContext` to use `DiagnosticGuard` (astral-sh#21587)
  [ty] Improve several "Did you mean?" suggestions (astral-sh#21597)
  [ty] Add more and update existing projects in `ty_benchmark` (astral-sh#21536)
  [ty] fix ty playground initialization and vite optimization issues (astral-sh#21471)
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.

2 participants