-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Make is_subtype_of exhaustive
#14924
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
|
7c953a8 to
41ba61a
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.
Awesome, thank you!!
41ba61a to
cdc9015
Compare
|
Thanks again! Would appreciate it if somebody could take another quick look before I land, since this is all quite subtle stuff |
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 to me!
Did you re-run the property tests on the latest version of the PR?
Co-authored-by: Carl Meyer <[email protected]>
8951d9b to
c433e88
Compare
|
Okay, I had to add a couple of new branches to This PR now causes no new property-test failures relative to The |
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, thank you!
Summary
This PR removes the fallback
_branch at the end ofType::is_subtype_of(): the function now uses an exhaustive match over the first element of the(self, target)pair. This means that we can have much more confidence that we've covered all cases, and it means that it's impossible to forget to update this function if we add more variants toTypein the future (which we will).The PR reworks our handling of most literal types in the
matchstatement so that they explicitly fallback to builtin types. This has several advantages:Type::ModuleLiteral("typing")is a subtype oftypes.ModuleTypeType::SliceLiteral([:2])is a subtype ofbuiltins.sliceType::Instance(Tuple)(which represents the static set of all possible tuples, so in Python type annotations it would be written astuple[object, ...])typing.Sequenceas soon as we understand generics -- we won't have to make any manual updates to this functionbuiltins.objectbeing the top type from the function; we now naturally understand this without any special-casing due to the fact that we understand that all classes haveobjectin their MROs.Test Plan
Several new subtyping tests have been added, and all existing tests pass.