Repository navigation
Do not prefer the reflected operator method for same-type operands - #22507
Open
zhengdaodeguL wants to merge 1 commit into
Open
zhengdaodeguL wants to merge 1 commit into
zhengdaodeguL wants to merge 1 commit into
Conversation
CPython gives the reflected method of the right operand priority only if the operands are of different types and the right operand's type is a subclass of the left's. check_op_reversible gave it priority whenever covers_at_runtime(right_type, left_type) held, but that predicate is also true for equal operand types, so for same-type operands with __lt__ and __gt__ defined in different classes of the MRO mypy inferred the result type of the wrong method. Keep covers_at_runtime rather than is_subtype: python#19006 relies on it for tuple[int, ...] vs a named tuple subclass. Add the different-types condition the data model requires. Fixes python#22386
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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 #22386
For
a < bwhereaandbhave exactly the same type, CPython callsa.__lt__(b); the reflected methodb.__gt__(a)only takes priority when the type ofbis a proper subclass of the type ofa. mypy tried the reflected method first whenever__lt__and__gt__were defined in different classes of the MRO, inferring the wrong method return type.This narrows the reflected-first shortcut in
check_op_reversibleso it does not apply when the operand types are the same (covers_at_runtimeis kept, since #19006 relies on it). The broader fix in #22402 (adding the operators toop_methods_that_shortcut) introduced false positives in mypy_primer (apache/spark, pandas); this narrower change is line-for-line equivalent to #22391, whose mypy_primer run showed zero impact on open-source code.Tests: new
testReverseBinaryOperatorSameTypeDoesNotGetPriorityincheck-expressions.test(issue repro plus a true-subclass case that still prefers the reflected method).pytest mypy/test/testcheck.py -k "check-expressions or check-operator": 203 passed.