Skip to content

Improve ordering comparisons in operator - #16812

Merged
srittau merged 5 commits into
python:mainfrom
carljm:cjm/operator-rich-comparisons
Nov 16, 2026
Merged

srittau merged 5 commits into
python:mainfrom
carljm:cjm/operator-rich-comparisons

Conversation

@carljm

@carljm carljm commented Nov 15, 2026

Copy link
Copy Markdown
Member

Since #16014, operator.lt(x, y) can fail type checking even when x < y is valid, when x.__lt__(y) returns an object with no __bool__ method. Python allows x < y to return that object directly, and operator.lt(x, y) has the same behavior.

Also, the current stubs for operator.lt, le, gt, and ge require both operands to define an ordering method. Python only requires one operand to handle the comparison.

Update operator.lt, le, gt, and ge to accept ordering methods with arbitrary return types and allow heterogeneous operands where either side implements the comparison.

Assisted by Codex.

@carljm
carljm marked this pull request as draft November 15, 2026 05:38
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@carljm
carljm marked this pull request as ready for review November 15, 2026 05:55
Comment thread stdlib/_operator.pyi Resolved
Comment thread stdlib/_operator.pyi Resolved
Comment thread stdlib/_operator.pyi Resolved
Comment thread stdlib/_operator.pyi Resolved
@carljm
carljm force-pushed the cjm/operator-rich-comparisons branch from 9eab27d to 573a07c Compare November 15, 2026 16:35
@carljm
carljm requested a review from AlexWaygood November 15, 2026 16:35
@github-actions

This comment has been minimized.

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment thread stdlib/_operator.pyi Resolved
@github-actions

Copy link
Copy Markdown
Contributor

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

pandera (https://github.com/pandera-dev/pandera)
+ pandera/backends/xarray/builtin_checks.py:404: error: Unused "type: ignore" comment  [unused-ignore]
+ pandera/backends/pandas/builtin_checks.py:195: error: Unused "type: ignore" comment  [unused-ignore]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/io/formats/style.py:4410: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/io/formats/style.py:4421: error: Unused "type: ignore" comment  [unused-ignore]

@srittau
srittau merged commit 772b45e into python:main Nov 16, 2026
88 checks passed
@carljm
carljm deleted the cjm/operator-rich-comparisons branch November 16, 2026 19:20
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.

3 participants