python / python/mypy

Tuple comparison does not check comparability of element types

Open
#21,042 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When comparing tuples, the comparability of the element isn't taken into account.

To Reproduce

from typing import *

class SupportsGT(Protocol):
    def __gt__(self, other: Any, /) -> bool:
        ...

a = cast(tuple[SupportsGT, ...], ...)
b = cast(tuple[object, ...], ...)
c = a < b  # no issue!

Expected Behavior

There is no matching a_elem.__lt__(b_elem) so this should fail.

Actual Behavior

No error.

Your Environment

Python 3.12
Mypy 1.19.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Python 3.12 reproduction in the issue and trace mypy's tuple-comparison type checking. Add a regression test showing that incompatible element comparisons are rejected, then run the relevant test suite to confirm the expected error is reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.