Skip to content

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Sep 9, 2025

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 str to be a subtype of Container[str] even though str.__contains__ is less permissive in the types it accepts than Container.__contains__; the reason they consider it a subtype nonetheless is that str has Container[str] in its MRO. We currently also consider str to be a subtype of Container[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 understand str as being a subtype of Container[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 to Iterable does not mean it is necessarily iterable -- it could be a Liskov-uncompliant subclass of Iterable! 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.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Sep 9, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 9, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Sep 9, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Base automatically changed from alex/hashable to main September 10, 2025 10:39
@AlexWaygood AlexWaygood enabled auto-merge (squash) September 10, 2025 11:02
@AlexWaygood AlexWaygood merged commit fd7eb1e into main Sep 10, 2025
37 checks passed
@AlexWaygood AlexWaygood deleted the alex/protocol-nominal branch September 10, 2025 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants