-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[red-knot] Implement disjointness for Instance types where the underlying class is @final
#15539
Conversation
…rlying class is `@final`
(Type::SubclassOf(subclass_of_ty), instance @ Type::Instance(_)) | ||
| (instance @ Type::Instance(_), Type::SubclassOf(subclass_of_ty)) => { | ||
// `type[T]` is disjoint from `S`, where `S` is an instance type, | ||
// if `U` is disjoint from `S`, | ||
// where `U` represents all instances of `T`'s metaclass | ||
let metaclass_instance = subclass_of_ty | ||
.subclass_of() | ||
.into_class() | ||
.map(|class| class.metaclass(db).to_instance(db)) | ||
.unwrap_or_else(|| KnownClass::Type.to_instance(db)); | ||
instance.is_disjoint_from(db, metaclass_instance) | ||
} |
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.
@carljm I realised almost as soon as our pairing session here that we were missing some subtle edge cases where a type[T]
type could still be disjoint from an instance type even if the instance type was a subtype of type
. So this logic is a little fiddlier than what we initially implemented. See the mdtest I've added for examples!
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.
Did a property test reveal this, or you just realized it looking at the code/tests?
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.
Just me reading the diff on github prior to filing the PR!
|
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.
Net reduction in lines of code (excluding tests), and improves correctness! Love to see it.
(Type::SubclassOf(subclass_of_ty), instance @ Type::Instance(_)) | ||
| (instance @ Type::Instance(_), Type::SubclassOf(subclass_of_ty)) => { | ||
// `type[T]` is disjoint from `S`, where `S` is an instance type, | ||
// if `U` is disjoint from `S`, | ||
// where `U` represents all instances of `T`'s metaclass | ||
let metaclass_instance = subclass_of_ty | ||
.subclass_of() | ||
.into_class() | ||
.map(|class| class.metaclass(db).to_instance(db)) | ||
.unwrap_or_else(|| KnownClass::Type.to_instance(db)); | ||
instance.is_disjoint_from(db, metaclass_instance) | ||
} |
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.
Did a property test reveal this, or you just realized it looking at the code/tests?
crates/red_knot_python_semantic/resources/mdtest/type_properties/is_disjoint_from.md
Outdated
Show resolved
Hide resolved
// `type[T]` is disjoint from `S`, where `S` is an instance type, | ||
// if `U` is disjoint from `S`, | ||
// where `U` represents all instances of `T`'s metaclass | ||
let metaclass_instance = subclass_of_ty | ||
.subclass_of() | ||
.into_class() | ||
.map(|class| class.metaclass(db).to_instance(db)) | ||
.unwrap_or_else(|| KnownClass::Type.to_instance(db)); | ||
instance.is_disjoint_from(db, metaclass_instance) |
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.
Nice! I like that we can delegate to instance-vs-instance, rather than checking finality here also.
It seems right that T
actually disappears from this check, apart from its metaclass. There's no difference between type[object]
and type[OtherClass]
as regards disjointness from an instance type, as long as the metaclass of OtherClass
is just type
.
…es/is_disjoint_from.md Co-authored-by: Carl Meyer <carl@astral.sh>
Summary
Closes #15508
For any two instance types
T
andS
, we know they are disjoint if eitherT
is final andT
is not a subclass ofS
orS
is final andS
is not a subclass ofT
.Correspondingly, for any two types
type[T]
andS
whereS
is an instance type,type[T]
can be said to be disjoint fromS
ifS
is disjoint fromU
, whereU
is the type that represents all instances ofT
's metaclass.And a heterogeneous tuple type can be said to be disjoint from an instance type if the instance type is disjoint from
tuple
(a type representing all instances of thetuple
class at runtime).Test Plan
is_disjoint_from()
tests are not written as mdtests just yet, but it's pretty hard to test some of these edge cases from a Rust unit test!QUICKCHECK_TESTS=1000000 cargo test --release -p red_knot_python_semantic -- --ignored types::property_tests::stable
Co-authored-by: Carl Meyer carl@astral.sh