Skip to content

Check element type comparability for tuple ordering comparisons#21107

Open
PGrayCS wants to merge 2 commits intopython:masterfrom
PGrayCS:fix-tuple-element-comparison
Open

Check element type comparability for tuple ordering comparisons#21107
PGrayCS wants to merge 2 commits intopython:masterfrom
PGrayCS:fix-tuple-element-comparison

Conversation

@PGrayCS
Copy link
Copy Markdown

@PGrayCS PGrayCS commented Mar 30, 2026

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 reverse tuple[object, ...].__gt__(tuple[SupportsGT, ...]) succeeds because tuple[SupportsGT, ...] is a subtype of tuple[object, ...]. At runtime this comparison would fail since object doesn't define __gt__.

Fix

After the tuple-level check_op succeeds for ordering operators, we now extract the element types from homogeneous tuples (tuple[X, ...]) and verify they support the comparison. If not, an Unsupported operand types error 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).

PGrayCS and others added 2 commits March 30, 2026 12:54
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
@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tuple comparison does not check comparability of element types

1 participant