-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[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
Conversation
4217f6b
to
9ef8f26
Compare
|
|
3f3fe04
to
d3fbb69
Compare
There was a problem hiding this 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.
d3fbb69
to
6fd65ce
Compare
6fd65ce
to
0144d7c
Compare
* 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)
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:Definition
because we don't create aDefinition
for modules (but we could?). Although I think it makes sense to possibly have a more public wrapper type anyway?Unknown
).An alternative design or an addition to this design is to introduce a
SemanticAnalysis(Db)
struct that has methods liketype_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 usingType
might be better after all)Test Plan
cargo test