Skip to content

Support special binary methods ("__add__" etc.) as Python does #2056

Closed

Description

@malinoff

I'm not sure if this is a bug report or a feature request, but as of cython 0.27.3 __radd__, __rtruediv__ and other right side magic methods do not work:

In [1]: %load_ext Cython

In [2]: %%cython
   ...: cdef class Example:
   ...:   def __radd__(self, other):
   ...:     print('radd', self, other)
   ...:   def __rtruediv__(self, other):
   ...:     print('rtruediv', self, other)
   ...:

In [3]: 3 + Example()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-b54d8dd2451f> in <module>()
----> 1 3 + Example()

TypeError: unsupported operand type(s) for +: 'int' and '_cython_magic_c1fde2d95cc29b87ed98f696ccfef975.Example'

In [4]: 'foo' / Example()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-1e29a35d9b28> in <module>()
----> 1 'foo' / Example()

TypeError: unsupported operand type(s) for /: 'str' and '_cython_magic_c1fde2d95cc29b87ed98f696ccfef975.Example'

In [5]: import Cython

In [6]: Cython.__version__
Out[6]: '0.27.3'

I'm using the following workaround, but it looks ugly:

In [7]: %%cython
   ...: cdef class Example:
   ...:   def __add__(other, self):
   ...:     print('add', self, other)
   ...:   def __truediv__(other, self):
   ...:     print('truediv', self, other)
   ...:

In [8]: 3 + Example()
add <_cython_magic_45c6eacd188f8181199b3c76f32cba5d.Example object at 0x109d7a0f0> 3

In [9]: 'foo' / Example()
truediv <_cython_magic_45c6eacd188f8181199b3c76f32cba5d.Example object at 0x109d7a110> foo

and doesn't really work if we switch operands:

In [10]: Example() + 3
add 3 <_cython_magic_45c6eacd188f8181199b3c76f32cba5d.Example object at 0x109d7a110>

In [11]: Example() / 'foo'
truediv foo <_cython_magic_45c6eacd188f8181199b3c76f32cba5d.Example object at 0x109468e60>

I'm not familiar with cython codebase, but a quick search through the source, issues and PRs doesn't show anything related.

Activity

  1. robertwb commented on Dec 30, 2017

    @robertwb
    Contributor
  2. added this to the 3.0 milestone on Nov 5, 2018
  3. robertwb commented on Nov 5, 2018

    @robertwb
    Contributor

    We should consider whether we want to move away from the "first or second argument is self" for the arithmetic methods for 3.0.

  4. scoder commented on Mar 27, 2019

    @scoder
    Contributor

    I think we should switch to normal Python semantics. As @robertwb said, in the same way as we support the comparison methods now.

  5. scoder commented on Mar 27, 2019

    @scoder
    Contributor

    See generate_richcmp_function() in ModuleNode.py together with the RichcmpSlot in TypeSlots.py.

    The idea is to define the number slots with a new slot type BinopSlot that has two special Python methods as implementation, and then generate the nb_add etc. slots by dispatching to these methods (more precisely, their C functions) according to CPython's own logic in typeobject.c.

  6. changed the title [-]Are the right side magic methods supposed to work?[/-] [+]Support special binary methods (`__add__` etc.) as Python does[/+] on Mar 27, 2019
  7. changed the title [-]Support special binary methods (`__add__` etc.) as Python does[/-] [+]Support special binary methods (__add__ etc.) as Python does[/+] on Mar 27, 2019
  8. changed the title [-]Support special binary methods (__add__ etc.) as Python does[/-] [+]Support special binary methods ("__add__" etc.) as Python does[/+] on Mar 27, 2019
  9. scoder commented on Apr 30, 2019

    @scoder
    Contributor

    Marking as Cython 3.0 release blocker, in case we need backwards incompatible changes for this.

  10. scoder commented on Jun 17, 2020

    @scoder
    Contributor

    The test failure in #3644 (in unicode_formatting.pyx) shows that changing default semantics will definitely break code. Potentially a lot of it since existing code bases could only implement the left-side operator and are now left without the right-side operator support (even in the case where the right-side operator support was the only thing they actually wanted).

    Do we really want to implement such a hard break? Is there a way we could mitigate the impact? Such as some kind of hybrid behaviour that would become the default instead of full Python semantics, maybe only if the right-side method is not implemented?

  11. robertwb commented on Jul 2, 2020

    @robertwb
    Contributor

    I would really like to unify the Python and Cython semantics here, rather than have our own thing (even if it's a step closer). One can get the old behavior with a directive; perhaps we could add a third option. Alternatively, we could introduce a warning (maybe this is a reason to backport to 0.29) about this change in behavior. The upside is the fix is really easy. (I would assume that people are expecting to take care when upgrading to 3.0, we should provide advice on how to do that, especially with defaults like this that change.)

  12. scoder commented on Jul 2, 2020

    @scoder
    Contributor

    I think a warning in 0.29 is a good idea, when users implement __add__ but not __radd__. They can do this in a backwards compatible way, by making __add__ support both directions and __radd__ only the reverse direction (which 0.29 will ignore then).

    I would really like to unify the Python and Cython semantics here, rather than have our own thing

    What do you find still missing?

  13. robertwb commented on Jul 3, 2020

    @robertwb
    Contributor

    I think a warning in 0.29 is a good idea, when users implement __add__ but not __radd__. They can do this in a backwards compatible way, by making __add__ support both directions and __radd__ only the reverse direction (which 0.29 will ignore then).

    That's a good idea.

    I would really like to unify the Python and Cython semantics here, rather than have our own thing

    What do you find still missing?

    I'm saying want to avoid re-introducing different behavior for cdef classes than for "normal" classes when a user implements __add__ but not __radd__.

  14. scoder commented on Jul 9, 2020

    @scoder
    Contributor

    I added a warning in ec99487 and also updated the documentation.

  15. scoder commented on Jul 9, 2020

    @scoder
    Contributor

    BTW, I think we do not assign the type of the self argument correctly yet, do we? That seems not so easy to change based on a directive, since the slot signatures are built statically.

  16. added a commit that references this issue on Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions