brettcannon/desugar

Binary arithmetic dispatch is not implemented correctly

eric-wieser opened this issue · 3 comments

With the following classes

class Foo:
    def __sub__(self, a):
        return ('sub', self, a)
    def __rsub__(self, a):
        return ('rsub', self, a)

class Bar(Foo):
    pass

desugar and CPython disagree on the result:

In [175]: from desugar.operator import sub

In [176]: sub(Foo(), Bar())
Out[176]: ('rsub', <__main__.Bar at 0x1f380795220>, <__main__.Foo at 0x1f380795b50>)

In [177]: Foo() - Bar()
Out[177]: ('sub', <__main__.Foo at 0x1f3804c6f40>, <__main__.Bar at 0x1f3804c6d00>)

See https://bugs.python.org/issue30140 for details - the conclusion was

Let's give up on this one.

Which led to the PR at python/cpython#1325 being closed.

Thanks for the heads-up. Looks like the docs were never updated to reflect this. I'll open a new issue to update the docs on docs.python.org.

I opened https://bugs.python.org/issue41584. If you read the current note just right, it's actually accurate and I read it wrong. But I plan to update the docs to be more explicit that the RHS subclass needs to provide an implementation different from the LHS.

Post is updated! Thanks, Eric!