-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Allow protocols to participate in nominal subtyping as well as structural subtyping #20314
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
Contributor
|
7e26957 to
c65bd20
Compare
4177ac0 to
07f55a7
Compare
c65bd20 to
ca79486
Compare
07f55a7 to
57b8b48
Compare
carljm
approved these changes
Sep 9, 2025
Contributor
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.
👍
AlexWaygood
commented
Sep 9, 2025
ca79486 to
c518a97
Compare
57b8b48 to
49bf994
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
(Stacked on top of #20284; review that PR first.)
Currently our implementation of subtyping and assignability checks against protocols only checks whether a given type is structurally a subtype of/assignable to a given protocol. Other type checkers, however, allow protocols to participate in nominal subtyping as well as structural subtyping. The most common example where this comes up is that all other type checkers consider
strto be a subtype ofContainer[str]even thoughstr.__contains__is less permissive in the types it accepts thanContainer.__contains__; the reason they consider it a subtype nonetheless is thatstrhasContainer[str]in its MRO. We currently also considerstrto be a subtype ofContainer[str], but this is because we still do not look at the types of method members when doing subtyping/assignability checks for protocols; an early version of #20165 revealed many ecosystem false positives due to our failure to understandstras being a subtype ofContainer[str].This PR therefore updates our subtyping/assignability logic for protocols so that, for a given class-based protocol, we also check whether a given other type could be considered a nominal subtype of that protocol before concluding that there exists no subtype relation between the other type and the protocol.
The implication of this is that one of our property tests must be removed:
all_type_assignable_to_iterable_are_iterable. Just because a type can be assignable toIterabledoes not mean it is necessarily iterable -- it could be a Liskov-uncompliant subclass ofIterable! I don't think we have any Liskov-uncompliant classes in our property-test generation right now, but this still seems like a potential cause for confusion if we added one (deliberately or not) in the future. As such, I've removed the property test as part of this PR.Test Plan
Mdtests added.