Skip to content

Fix same-type comparison method resolution - #22402

Open
2Zest wants to merge 1 commit into
python:masterfrom
2Zest:fix/same-type-comparison-order-22386
Open

2Zest wants to merge 1 commit into
python:masterfrom
2Zest:fix/same-type-comparison-order-22386

Conversation

@2Zest

@2Zest 2Zest commented Nov 9, 2026

Copy link
Copy Markdown

Summary

  • Prefer the forward comparison method when operands have the same type, matching Python's runtime dispatch order.
  • Add a regression test for inherited lt and subclass-defined gt methods.

Testing

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest -o addopts="" mypy/test/testcheck.py::TypeCheckSuite::check-expressions.test (203 passed)
  • python -m ruff check mypy/operators.py
  • git diff --check

Fixes #22386

Co-authored-by: Codex <noreply@openai.com>
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

spark (https://github.com/apache/spark)
+ python/pyspark/core/rdd.py:2346: error: Unsupported left operand type for > ("S")  [operator]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/indexes/datetimelike.py:503: error: Unsupported left operand type for > ("Resolution")  [operator]

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.

comparison of two operands of the same type calls the reflected method first when __lt__ and __gt__ are defined in different classes

1 participant