ruff
942e781b - [ty] Share definition resolution between type inference and IDE features (#28032)

Commit
36 days ago
[ty] Share definition resolution between type inference and IDE features (#28032) ## 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](https://github.com/astral-sh/ruff/pull/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.
Author
Parents
Loading