Fix overload inference with protocol __class__ overrides - #21896
Open
daleselaji-dev wants to merge 1 commit into
Open
Fix overload inference with protocol __class__ overrides#21896daleselaji-dev wants to merge 1 commit into
daleselaji-dev wants to merge 1 commit into
Conversation
daleselaji-dev
marked this pull request as ready for review
August 27, 2026 03:42
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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
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.
Problem
When a protocol overrides
__class__, overload inference can select an incompatible overload. A concretelist[int]was incorrectly inferred as matching a protocol overload parameterized withstr, so the call result was revealed asstrinstead ofint.Root Cause
Protocol constraint inference treated the synthetic
__class__member as a normal structural member. Because every object exposes__class__, its concrete class object could contribute an unrelated constraint for the protocol type variable and distort overload selection.Solution
Ignore
__class__when inferring type-variable constraints from protocol members. It remains available for normal member lookup and structural compatibility checks; it is excluded only from constraint generation, where it cannot provide a useful protocol relationship.Changes
__class__ininfer_constraints_from_protocol_members.__class__override.Testing
py -3.10 -m pytest -n0 -q mypy/test/testsubtypes.py -k protocol_overload_with_class_override(passed: 1)py -3.10 -m pytest -n0 -q mypy/test/testsubtypes.py(passed: 27)py -3.10 -m black --check mypy/constraints.py mypy/test/testsubtypes.py(passed)py -3.10 -m ruff check mypy/constraints.py mypy/test/testsubtypes.py(passed)py -3.10 -m compileall -q mypy/constraints.py mypy/test/testsubtypes.py(passed)git diff --check(passed)pre-commitwas attempted but could not complete because the first-time actionlint environment download returnedBadZipFile; this remains unverified.Compatibility/Risk
The change is limited to protocol constraint inference and does not alter runtime behavior. The main risk is an unexpected effect on a protocol that intentionally relies on
__class__for type-variable inference; the regression test and full subtype suite cover the affected path.Notes for Reviewer
Please review whether
__class__should be excluded from protocol constraint inference globally or only for this overload-inference path, and consider adding any project-specific edge cases to the subtype test suite.Linked Issue
Closes #21795