Repository navigation
Conversation
When a comparison falls back to (or prefers) a reflected dunder method (for example, checking int.__le__ for x >= 0 when x is int | None), MessageBuilder.incompatible_argument previously rendered the reflected operator symbol and reversed operand order because comparison methods do not use an __r prefix. Track the active reflected operator context and original symbol in MessageBuilder while ExpressionChecker.check_op_reversible checks a reflected variant, so error messages report the written operator and source operand order. Fixes python#22316. Co-Authored-By: Claude Code <noreply@anthropic.com>
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: spark (https://github.com/apache/spark)
- python/pyspark/pandas/groupby.py:770: error: Unsupported operand types for >= ("int" and "Number") [operator]
+ python/pyspark/pandas/groupby.py:770: error: Unsupported operand types for <= ("Number" and "int") [operator]
prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/cli/work_queue.py:779: error: Unsupported operand types for > ("datetime" and "None") [operator]
+ src/prefect/cli/work_queue.py:779: error: Unsupported operand types for < ("None" and "datetime") [operator]
discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/tasks/__init__.py:232: error: Unsupported operand types for >= ("datetime" and "None") [operator]
+ discord/ext/tasks/__init__.py:232: error: Unsupported operand types for <= ("None" and "datetime") [operator]
pyodide (https://github.com/pyodide/pyodide)
- pyodide-build/pyodide_build/tests/test_xbuildenv.py:573: error: Unsupported operand types for <= ("str" and "None") [operator]
+ pyodide-build/pyodide_build/tests/test_xbuildenv.py:573: error: Unsupported operand types for >= ("None" and "str") [operator]
- pyodide-build/pyodide_build/tests/test_xbuildenv.py:576: error: Unsupported operand types for <= ("str" and "None") [operator]
+ pyodide-build/pyodide_build/tests/test_xbuildenv.py:576: error: Unsupported operand types for >= ("None" and "str") [operator]
scikit-learn (https://github.com/scikit-learn/scikit-learn)
- sklearn/externals/array_api_compat/numpy/_aliases.py:164: error: Unsupported operand types for >= ("int" and "None") [operator]
+ sklearn/externals/array_api_compat/numpy/_aliases.py:164: error: Unsupported operand types for <= ("None" and "int") [operator]
- sklearn/externals/array_api_compat/numpy/_aliases.py:166: error: Unsupported operand types for > ("int" and "None") [operator]
+ sklearn/externals/array_api_compat/numpy/_aliases.py:166: error: Unsupported operand types for < ("None" and "int") [operator]
- sklearn/externals/array_api_compat/numpy/_aliases.py:175: error: Unsupported operand types for <= ("int" and "None") [operator]
+ sklearn/externals/array_api_compat/numpy/_aliases.py:175: error: Unsupported operand types for >= ("None" and "int") [operator]
- sklearn/externals/array_api_compat/numpy/_aliases.py:177: error: Unsupported operand types for < ("int" and "None") [operator]
+ sklearn/externals/array_api_compat/numpy/_aliases.py:177: error: Unsupported operand types for > ("None" and "int") [operator]
jax (https://github.com/google/jax)
- jax/experimental/mosaic/gpu/fragmented_array.py:5546: error: Unsupported operand types for < ("int" and "object") [operator]
+ jax/experimental/mosaic/gpu/fragmented_array.py:5546: error: Unsupported operand types for > ("object" and "int") [operator]
- jax/experimental/mosaic/gpu/fragmented_array.py:5656: error: Unsupported operand types for < ("int" and "object") [operator]
+ jax/experimental/mosaic/gpu/fragmented_array.py:5656: error: Unsupported operand types for > ("object" and "int") [operator]
apprise (https://github.com/caronc/apprise)
- apprise/persistent_store.py:1528: error: Unsupported operand types for > ("datetime" and "None") [operator]
+ apprise/persistent_store.py:1528: error: Unsupported operand types for < ("None" and "datetime") [operator]
- apprise/plugins/amqp/base.py:414: error: Unsupported operand types for >= ("int" and "object") [operator]
+ apprise/plugins/amqp/base.py:414: error: Unsupported operand types for <= ("object" and "int") [operator]
- apprise/plugins/mobilemessage.py:255: error: Unsupported operand types for >= ("int" and "object") [operator]
+ apprise/plugins/mobilemessage.py:255: error: Unsupported operand types for <= ("object" and "int") [operator]
cvxpy (https://github.com/cvxpy/cvxpy)
- cvxpy/expressions/expression.py:424: error: Unsupported operand types for < ("int" and "generic[Any]") [operator]
+ cvxpy/expressions/expression.py:424: error: Unsupported operand types for > ("generic[Any]" and "int") [operator]
- cvxpy/expressions/expression.py:424: error: Unsupported operand types for < ("int" and "sparray[Any, tuple[Any, ...]]") [operator]
+ cvxpy/expressions/expression.py:424: error: Unsupported operand types for > ("sparray[Any, tuple[Any, ...]]" and "int") [operator]
- cvxpy/expressions/expression.py:424: error: Unsupported operand types for < ("int" and "complex") [operator]
+ cvxpy/expressions/expression.py:424: error: Unsupported operand types for > ("complex" and "int") [operator]
- cvxpy/expressions/constants/constant.py:134: error: Unsupported operand types for < ("int" and "None") [operator]
+ cvxpy/expressions/constants/constant.py:134: error: Unsupported operand types for > ("None" and "int") [operator]
- cvxpy/utilities/power_tools.py:175: error: Unsupported operand types for < ("int" and "Integral") [operator]
+ cvxpy/utilities/power_tools.py:175: error: Unsupported operand types for > ("Integral" and "int") [operator]
- cvxpy/utilities/power_tools.py:197: error: Unsupported operand types for <= ("int" and "Integral") [operator]
+ cvxpy/utilities/power_tools.py:197: error: Unsupported operand types for >= ("Integral" and "int") [operator]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22316
When checking a comparison falls back to (or prioritizes) a reflected method — for example, checking
int.__le__forx >= 0wherex: int | None, or checking a subclass's reflected__gt__/__eq__/__ne__—MessageBuilder.incompatible_argumentpreviously reported the reflected operator symbol and reversed operand order:Previously:
Now:
Summary of changes
(context, op)onMessageBuilderwhileExpressionChecker.check_op_reversibleevaluates a reflected variant (with zero overhead on the forward path).MessageBuilder.incompatible_argument, when the failing call matches the active reflected operatorouter_context, report the written operator symbol and source operand order (arg_typethenbase), matching how reflected arithmetic operators (__radd__, etc.) are already reported.<,<=,>,>=with unions/None/chained comparisons, custom classes, reflected subclass overrides (<,==,!=), explicit dunder calls, and arithmetic operators.check-expressions.test,check-plugin-attrs.test, andcheck-incremental.testthat previously asserted the mirrored operator and swapped operand order.Verification
masterfor<,<=,>,>=and reflected subclass overrides.pytest -n 8 mypy/test/testcheck.py: 8363 passed, 16 skipped, 7 xfailed, 0 failed.testpythoneval.py,testfinegrained.py,testcmdline.py,testoutput.py,testformatter.py,testerrorstream.py,testreports.py,testfinegrainedcache.py: 0 failed.python -m mypy --config-file mypy_self_check.ini mypy: 0 issues across 195 source files.black --checkandruff check: passed.🤖 Generated with Claude Code