-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Move InstanceType to its own submodule
#17525
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
|
carljm
left a comment
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.
Changes here look fine to me either way. I'm curious to learn more about the pros and cons of a separate Type::ProtocolInstance vs reusing Type::Instance.
To make sure I understand the alternative you're suggesting: |
No, I was asking why we need an enum or I do think there are likely advantages in clarity (and maybe even in performance) to having a separate variant, because it means we don't have to check |
…struct an instance of `InstanceType` is now through the `Type::instance()` method.
…prove encapsulation.
e1d5369 to
2f5d2dd
Compare
Ah, sorry. Perhaps I should have written up a short design doc for this. I remember discussing this plan with you in a 1:1, but that was probably a couple of weeks ago by now, and that anyway wasn't a conversation that was visible to the rest of the team :-( The first reason why I think it makes sense for A second reason is that, as we've discussed in the past, I think we will have to support synthesized protocols as well as protocols that point to a specific class definition. This will be important for normalizing protocols into types that share the same Salsa ID if they are in fact equivalent to each other, which is necessary for our implementation of ruff/crates/red_knot_python_semantic/resources/mdtest/protocols.md Lines 650 to 664 in d2b20f7
So I'm imagining that the enum ProtocolInstanceType<'db> {
FromClass(ClassType<'db>),
Synthesized(SynthesizedProtocolType<'db>),
}
#[salsa::tracked]
struct SynthesizedProtocolType<'db> {
members: Box<[ProtocolMember<'db>]>,
}As we've also discussed in the past, this also has the advantage that we will in the future be able to consider removing the |
2f5d2dd to
b0f07a1
Compare
* main: (37 commits) [red-knot] Add list of failing/slow ecosystem projects (#17474) [red-knot] mypy_primer: extend ecosystem checks (#17544) [red-knot] Move `InstanceType` to its own submodule (#17525) [red-knot] mypy_primer: capture backtraces (#17543) [red-knot] mypy_primer: Use upstream repo (#17500) [red-knot] `typing.dataclass_transform` (#17445) Update dependency react-resizable-panels to v2.1.8 (#17513) Update dependency smol-toml to v1.3.3 (#17505) Update dependency uuid to v11.1.0 (#17517) Update actions/setup-node action to v4.4.0 (#17514) [red-knot] Fix variable name (#17532) [red-knot] Add basic subtyping between class literal and callable (#17469) [`pyupgrade`] Add fix safety section to docs (`UP030`) (#17443) [`perflint`] Allow list function calls to be replaced with a comprehension (`PERF401`) (#17519) Update pre-commit dependencies (#17506) [red-knot] Simplify visibility constraint handling for `*`-import definitions (#17486) [red-knot] Detect (some) invalid protocols (#17488) [red-knot] Correctly identify protocol classes (#17487) Update dependency ruff to v0.11.6 (#17516) Update Rust crate shellexpand to v3.1.1 (#17512) ...
Summary
This PR moves red-knot's
InstanceTypestruct out ofclass.rsand into its own submodule. TheInstanceType::classfield is made private; the only way of creatingInstanceTypes is now through theType::instance()method (which is also moved to the newinstance.rssubmodule).The motivation for this change is that it helps pave the way for adding a new
Type::ProtocolInstancevariant. When we have such a variant, I'm envisaging that theType::instance()method would change to look something like this:Since the
Type::instance()method would be the only public constructor for bothInstanceTypeandProtocolInstanceType, this would guarantee the invariant that nominal instance types would always be represented in our model usingType::Instancevariants, andProtocolinstance types would always be represented usingType::ProtocolInstancevariants.I also moved the
KnownInstanceTypeenum to its own submodule in this PR, as it has very little to do with the other types inclass.rs, and has equally little to do with eitherInstanceTypeor theProtocolInstanceTypethat live (/will live) ininstance.rs.This PR is best reviewed commit by commit! I tried to include helpful commit messages explaining what each commit does.
Test Plan
cargo test -p red_knot_python_semantic