Skip to content

[red-knot] Add Type.definition method #17153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Apr 2, 2025

Summary

This is a follow up to the goto type definition PR. Specifically, that we want to avoid exposing too many semantic model internals publicly.

I want to get some feedback on the approach taken. I think it goes into the right direction but I'm not super happy with it.
The basic idea is that we add a Type::definition method which does the "goto type definition". The parts that I think make it awkward:

  • We can't directly return Definition because we don't create a Definition for modules (but we could?). Although I think it makes sense to possibly have a more public wrapper type anyway?
  • It doesn't handle unions and intersections. Mainly because not all elements in an intersection may have a definition and we only want to show a navigation target for intersections if there's only a single positive element (besides maybe Unknown).

An alternative design or an addition to this design is to introduce a SemanticAnalysis(Db) struct that has methods like type_definition(&self, type) which explicitly exposes the methods we want. I don't feel comfortable design this API yet because it's unclear how fine granular it has to be (and if it is very fine granular, directly using Type might be better after all)

Test Plan

cargo test

@MichaReiser MichaReiser force-pushed the micha/type-definition branch from 4217f6b to 9ef8f26 Compare April 2, 2025 14:55
@MichaReiser MichaReiser added the ty Multi-file analysis & type inference label Apr 2, 2025
Copy link
Contributor

github-actions bot commented Apr 2, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

github-actions bot commented Apr 2, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser MichaReiser force-pushed the micha/type-definition branch 3 times, most recently from 3f3fe04 to d3fbb69 Compare April 2, 2025 15:45
@MichaReiser MichaReiser marked this pull request as ready for review April 2, 2025 16:03
@AlexWaygood AlexWaygood removed their request for review April 2, 2025 17:55
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

This seems fine to me, and I'm happy to see a lot of unnecessary pub removed.

I think of Definition as a pretty internal implementation detail of the semantic index and symbol resolution, so I'd prefer if it weren't pub at all, and I'm happy to have a separate TypeDefinition struct exposed as public API.

I don't think it would make sense to have Definition for a module, because the meaning of Definition is a value assigned to a name in a scope, which doesn't apply to an entire module.

Leaving the caller to decide how to handle intersections and unions also seems reasonable to me; the union or intersection type does not itself have a definition location. I guess the main disadvantage I see here is that it requires us to expose more of the API of union and intersection types, but we probably will need this for the linter anyway.

@MichaReiser MichaReiser force-pushed the micha/type-definition branch from d3fbb69 to 6fd65ce Compare April 4, 2025 08:18
@MichaReiser MichaReiser force-pushed the micha/type-definition branch from 6fd65ce to 0144d7c Compare April 4, 2025 08:28
@MichaReiser MichaReiser merged commit ffa824e into main Apr 4, 2025
23 checks passed
@MichaReiser MichaReiser deleted the micha/type-definition branch April 4, 2025 08:56
dcreager added a commit that referenced this pull request Apr 4, 2025
* main:
  [red-knot] Empty tuple is always-falsy (#17213)
  Run fuzzer with `--preview` (#17210)
  Bump 0.11.4 (#17212)
  [syntax-errors] Allow `yield` in base classes and annotations (#17206)
  Don't skip visiting non-tuple slice in `typing.Annotated` subscripts (#17201)
  [red-knot] mypy_primer: do not specify Python version (#17200)
  [red-knot] Add `Type.definition` method (#17153)
  Implement `Invalid rule provided` as rule RUF102 with `--fix` (#17138)
  [red-knot] Add basic on-hover to playground and LSP (#17057)
  [red-knot] don't remove negations when simplifying constrained typevars (#17189)
  [minor] Fix extra semicolon for clippy (#17188)
  [syntax-errors] Invalid syntax in annotations (#17101)
  [syntax-errors] Duplicate attributes in match class pattern (#17186)
  [syntax-errors] Fix multiple assignment for class keyword argument (#17184)
  use astral-sh/cargo-dist instead (#17187)
  Enable overindented docs lint (#17182)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants