Skip to content

Improve ordering comparisons in operator - #16812

Open
carljm wants to merge 4 commits into
python:mainfrom
carljm:cjm/operator-rich-comparisons
Open

carljm wants to merge 4 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

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]

@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
Comment on lines +25 to +26
# operator returns the rich comparison result without testing its truth value, so these protocols
# return `object` (unlike the similar protocols in `_typeshed`).

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.

Suggested change
# operator returns the rich comparison result without testing its truth value, so these protocols
# return `object` (unlike the similar protocols in `_typeshed`).
# Unlike other stdlib APIs, APIs in the `operator` module return the result of calling a rich-comparison
# method without testing the truth value of that result. Thus, whereas `_typeshed.SupportsDunderLT`
# (a general-purpose API used in multiple locations) returns `SupportsBool`, it's sufficient for
# the version here to return `object`.

This branch has not been deployed

No deployments
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.

2 participants