Check element type comparability for tuple ordering comparisons#21107
Open
PGrayCS wants to merge 2 commits intopython:masterfrom
Open
Check element type comparability for tuple ordering comparisons#21107PGrayCS wants to merge 2 commits intopython:masterfrom
PGrayCS wants to merge 2 commits intopython:masterfrom
Conversation
When comparing tuples with ordering operators (<, >, <=, >=), mypy now verifies that the element types actually support the comparison operator. Previously, due to tuple's covariant TypeVar, the reverse operator could pass at the type level even when element types don't support the ordering comparison at runtime. For example, tuple[object, ...].__gt__ would accept any tuple argument via covariance, but object doesn't define __gt__. The fix adds element-level validation after the tuple-level check succeeds: if the element types can't be compared with the same operator, an 'Unsupported operand types' error is reported. Fixes python#21042
for more information, see https://pre-commit.ci
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.
Fixes #21042
When comparing tuples with ordering operators (
<,>,<=,>=), mypy now verifies that the element types actually support the comparison operator.Problem
The typeshed stubs for tuple define comparison methods using the class-level covariant TypeVar
_T_co:python
def lt(self, value: tuple[_T_co, ...], /) -> bool: ...
Due to covariance, when checking e.g.
tuple[SupportsGT, ...] < tuple[object, ...], the forward__lt__fails (correct), but the reversetuple[object, ...].__gt__(tuple[SupportsGT, ...])succeeds becausetuple[SupportsGT, ...]is a subtype oftuple[object, ...]. At runtime this comparison would fail sinceobjectdoesn't define__gt__.Fix
After the tuple-level
check_opsucceeds for ordering operators, we now extract the element types from homogeneous tuples (tuple[X, ...]) and verify they support the comparison. If not, anUnsupported operand typeserror is reported.Test case from the issue:
python
from typing import Any, Protocol, cast
class SupportsGT(Protocol):
def gt(self, other: Any, /) -> bool: ...
a = cast(tuple[SupportsGT, ...], ...)
b = cast(tuple[object, ...], ...)
c = a < b # now errors: Unsupported operand types for < (...)
All existing tests pass (7980 check tests, 0 regressions).