Try reflected binary-op dunder first for proper subclasses (#3876)#3982
Open
durvesh1992 wants to merge 1 commit into
Open
Try reflected binary-op dunder first for proper subclasses (#3876)#3982durvesh1992 wants to merge 1 commit into
durvesh1992 wants to merge 1 commit into
Conversation
Python's data model calls the reflected dunder first when the right operand's type is a proper subclass of the left operand's type. Pyrefly always tried the forward dunder first, so `int_val & some_IntFlag_member` resolved through `int.__and__` and widened the result back to `int` instead of keeping the flag type via `IntFlag.__rand__` (facebook#3876). Reorder the binary-op candidates so the reflected dunder is tried first in that subclass case. A subclass that does not override the reflected dunder inherits it unchanged and try_binop_calls still falls back to the forward dunder, so non-overriding subclasses are unaffected.
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.
Summary
Fixes #3876.
Per Python's data model, when the right operand's type is a proper subclass of the left operand's type, the reflected dunder is tried first. Pyrefly always tried the forward dunder first, so for example:
int & Colorresolved throughint.__and__(which widens the result back toint) instead ofColor.__rand__(which returns the flag typeSelf). At runtimeint_val & colorcallsColor.__rand__and yields aColor.Fix
In
binop_types'binop_call(pyrefly/lib/alt/operators.rs), when the RHS type is a proper subclass of the LHS type, the reflected dunder is now tried first.This is intentionally narrow:
rhsis a strict subclass oflhs(has_superclass+ not equal).try_binop_callsstill falls back to the forward dunder if the reflected call doesn't apply — so non-overriding subclasses are unaffected.Test
Added
test_reflected_dunder_subclass_priorityinpyrefly/lib/test/operators.rsassertingx & c,x | c,x ^ careColorforx: int,c: Color. It fails before this change (inferredint) and passes after.