mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:45:24 +00:00
[red-knot] Fix edge case for binary-expression inference where the lhs and rhs are the exact same type (#13823)
## Summary This fixes an edge case that @carljm and I missed when implementing https://github.com/astral-sh/ruff/pull/13800. Namely, if the left-hand operand is the _exact same type_ as the right-hand operand, the reflected dunder on the right-hand operand is never tried: ```pycon >>> class Foo: ... def __radd__(self, other): ... return 42 ... >>> Foo() + Foo() Traceback (most recent call last): File "<python-input-1>", line 1, in <module> Foo() + Foo() ~~~~~~^~~~~~~ TypeError: unsupported operand type(s) for +: 'Foo' and 'Foo' ``` This edge case _is_ covered in Brett's blog at https://snarky.ca/unravelling-binary-arithmetic-operations-in-python/, but I missed it amongst all the other subtleties of this algorithm. The motivations and history behind it were discussed in https://mail.python.org/archives/list/python-dev@python.org/thread/7NZUCODEAPQFMRFXYRMGJXDSIS3WJYIV/ ## Test Plan I added an mdtest for this cornercase.
This commit is contained in:
parent
f4b5e70fae
commit
55bccf6680
2 changed files with 27 additions and 4 deletions
|
@ -2682,10 +2682,14 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
.call(self.db, &[left_ty, right_ty])
|
||||
.return_ty(self.db)
|
||||
.or_else(|| {
|
||||
right_class
|
||||
.class_member(self.db, op.reflected_dunder())
|
||||
.call(self.db, &[right_ty, left_ty])
|
||||
.return_ty(self.db)
|
||||
if left_class == right_class {
|
||||
None
|
||||
} else {
|
||||
right_class
|
||||
.class_member(self.db, op.reflected_dunder())
|
||||
.call(self.db, &[right_ty, left_ty])
|
||||
.return_ty(self.db)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue