-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Fixup a few edge cases regarding type[]
#14918
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
a15423a to
0db080d
Compare
|
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.
Thanks. I think I do prefer always explicitly unpacking to a Class, and avoiding methods that blur the line between a ClassBase and a Class.
|
Okay... I tried to add some more comments to the function... and ended up writing #14924... which I will have to finish tomorrow... |
|
I think tomorrow I'll rip out the |
Sounds fine, though I also think it would be fine if this PR at least removes the |
|
Sure |
type[Any]type[]
db08069 to
ee540c7
Compare
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.
Looks good!
| Intersection { | ||
| pos: Vec<Ty>, | ||
| neg: Vec<Ty>, | ||
| }, |
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.
minor nit, but it doesn't seem necessary to explode this onto multiple lines? Is rustfmt doing that because the trailing comma was added?
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.
no idea why rustfmt is doing it! I can see if I can get rustfmt to undo it
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.
I cannot get rustfmt to undo it. Ours not to reason why?
Summary
There's a functional change here, and a few cosmetic changes.
The functional change is that we currently consider
Literal[str]to be disjoint fromtype[Any]. But they're not disjoint:Anyhere could be materialized asstrorobject; in those materializations, the two types would clearly overlap, and thereforetype[Any]overlaps withLiteral[str]. This PR fixes our logic inType::is_disjoint_fromto reflect that.Also included in this PR are cosmetic changes to
Type::is_subtype_of. The main reason for these changes is that currently we haveType::SubclassOf(target_class))branches inmatchstatements where thetarget_classvariable actually has typeClassBaserather thanClass; this is quite confusing. A secondary reason for these changes is that it should be impossible for us to encounterType::SubclassOf(SubclassOfType { base })instances in thismatchstatement wherebaseis not aClassBase::Class()variant, since all otherClassBase::Class()variants are not fully static exit, and therefore we immediately early inis_subtype_of()here if we encounter one of them:ruff/crates/red_knot_python_semantic/src/types.rs
Lines 607 to 612 in 6e11086
The changes to
Type::is_subtype_of()clarify that we shouldn't be dealing with any otherClassBase::Class()variants in thismatchstatement; we should therefore be usingClass::is_subclass_of()rather thanClass:is_subclass_of_base().As a result of the above changes,
Class::is_subclass_of_base()actually becomes unused, so this PR removes it as well. I think this is probably also for the best: aClassBaseand aClassare two different things, and it feels like it blurs the two concepts to have a method that asks whether a class is a "subclass of" aClassBase. If we need to ask this question again in the future, it's not that much more verbose to just doclass.mro(db).contains(&base).Test Plan
cargo test -p red_knot_python_semantic. The new test case for theis_not_disjoint_fromtest is the only one added in this PR that fails onmain, but the other added test cases seem useful anyway.