Skip to content

[ty] Share definition resolution between type inference and IDE features - #28032

Open
AlexWaygood wants to merge 1 commit into
mainfrom
alex/refactor-goto-definition
Open

[ty] Share definition resolution between type inference and IDE features#28032
AlexWaygood wants to merge 1 commit into
mainfrom
alex/refactor-goto-definition

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Aug 25, 2026

Copy link
Copy Markdown
Member

Summary

This is a behaviour-preserving refactor of definition resolution: navigation targets, docstrings, and existing type-checking results are preserved. The changes are to the API boundaries and the inference work performed internally.

LSP benefits

Go-to-definition and go-to-declaration for ordinary builtins such as isinstance no longer request completed inference of the enclosing scope just to check whether numeric-annotation expansion applies. Only float and complex need that check. Completing scope inference computes and caches types for the scope's expressions, along with diagnostics and other inference data, even though navigation only needs the symbol's definition.

The expected benefit is lower navigation latency when scope inference is uncached or has been invalidated by an edit, plus fewer temporary allocations and fewer inference results retained solely for navigation. The savings depend on the workload: cached inference leaves little computation to avoid, and diagnostics may still require the same cached data. This does not guarantee lower steady-state memory usage for the server.

Reuse during type inference

The shared API will support inference-side consumers such as #27634, whose redundant-condition analysis needs source definitions as well as inferred types:

  • It follows assignments and imports to recognize conditions derived from sys.version_info, sys.platform, os.name, or typing.TYPE_CHECKING. These can be constant for the configured target while still being deliberate compatibility guards, including when accessed through aliases or attributes.
  • It finds the annotation behind an always-truthy tuple so the diagnostic can point to tuple[T] and suggest tuple[T, ...] when a tuple of arbitrary length may have been intended.

These checks run while the enclosing scope is still being inferred. Using the IDE API to obtain expression types at that point can re-enter inference of the same scope, causing avoidable Salsa query cycles and repeated work. The shared helpers instead accept an explicit scope or an already-inferred receiver type, allowing these checks to reuse the existing name, import, and member-resolution rules without requesting completed inference of the current scope. Pydantic's existing annotation-alias lookup is also migrated to this API.

Diff overview

  • Moved: Existing scope traversal, class/MRO member lookup, import resolution, and resolved-definition/docstring helpers move into definition_resolution, preserving their lookup algorithms. Numeric-annotation expansion and stub-to-implementation mapping remain in ide_support.
  • Added: Shared lookup entry points and three regression tests that assert definition lookup does not request completed scope inference.
  • Changed: IDE wrappers and the Pydantic caller delegate to the shared helpers; builtin name lookup gets the float/complex inference guard described above. The remaining edits adapt parameters, imports, and visibility, and document the shared API's contracts.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Aug 25, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 97.79%. The percentage of expected errors that received a diagnostic held steady at 94.33%. The number of fully passing files held steady at 112/136.

@astral-sh-bot

astral-sh-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 40.22MB 40.22MB -
sphinx 172.66MB 172.66MB -
trio 96.94MB 96.94MB -
prefect 477.68MB 477.57MB -0.02% (114.05kB) ⬇️

Significant changes

Click to expand detailed breakdown

prefect

Name Old New Diff Outcome
parsed_module 87.81MB 87.78MB -0.04% (32.03kB) ⬇️
semantic_index 93.21MB 93.19MB -0.02% (20.51kB) ⬇️
infer_scope_types_impl 19.16MB 19.15MB -0.07% (12.78kB) ⬇️
infer_deferred_types 4.56MB 4.55MB -0.24% (11.18kB) ⬇️
StringLiteralType 4.46MB 4.45MB -0.14% (6.47kB) ⬇️
infer_function_default_types 549.79kB 545.44kB -0.79% (4.35kB) ⬇️
Definition 18.14MB 18.14MB -0.02% (4.06kB) ⬇️
infer_definition_types 37.09MB 37.08MB -0.01% (3.99kB) ⬇️
source_text 22.74MB 22.74MB -0.01% (3.27kB) ⬇️
StaticClassLiteral<'db>::own_fields_inner_ 916.81kB 914.03kB -0.30% (2.78kB) ⬇️
File 640.16kB 638.71kB -0.23% (1.44kB) ⬇️
Expression 6.95MB 6.95MB -0.02% (1.31kB) ⬇️
place_by_id 3.74MB 3.73MB -0.02% (912.00B) ⬇️
BoundTypeVarInstance 1.61MB 1.61MB -0.05% (880.00B) ⬇️
place_by_id::interned_arguments 2.85MB 2.85MB -0.02% (720.00B) ⬇️
... 48 more

@astral-sh-bot

astral-sh-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@AlexWaygood AlexWaygood added the internal An internal refactor or improvement label Aug 25, 2026
@AlexWaygood
AlexWaygood marked this pull request as ready for review August 25, 2026 14:27
@AlexWaygood
AlexWaygood requested a review from a team as a code owner August 25, 2026 14:27
@astral-sh-bot
astral-sh-bot Bot requested a review from sharkdp August 25, 2026 14:27
@MichaReiser
MichaReiser requested a review from lerebear August 25, 2026 14:29
@MichaReiser

Copy link
Copy Markdown
Member

I'll add @lerebear as reviewer, given that he has been thinking about how to share more semantic APIs with the LSP

@codspeed-hq

codspeed-hq Bot commented Aug 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 126 untouched benchmarks
⏩ 84 skipped benchmarks1


Comparing alex/refactor-goto-definition (e980072) with main (b52fe1b)

Open in CodSpeed

Footnotes

  1. 84 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Base automatically changed from alex/always-truthy-tests to main August 26, 2026 22:38
@AlexWaygood
AlexWaygood force-pushed the alex/refactor-goto-definition branch from 1bcca85 to e980072 Compare August 26, 2026 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants