You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support special binary methods ("__add__" etc.) as Python does #2056
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:
Cython's special methods still mostly reflect the C API rather than the
Python API. See
http://cython.readthedocs.io/en/latest/src/userguide/special_methods.html#arithmetic-methods
. Here either the first or second parameter may be self, rather than having
__rxxx__ versions of the operators. (It would be nice to support this
however, similar to how the comparison methods were recently added.)
On Thu, Dec 28, 2017 at 3:14 AM, Dmitry Malinovsky ***@***.*** > wrote:
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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2056>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAdqgcvFpRoV1V5VQmmG1gnKEIwYNzK5ks5tE3gWgaJpZM4ROO6o>
.
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.
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
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
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
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?
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.)
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
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__.
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.
Activity
robertwb commentedon Dec 30, 2017
robertwb commentedon Nov 5, 2018
We should consider whether we want to move away from the "first or second argument is self" for the arithmetic methods for 3.0.
scoder commentedon Mar 27, 2019
I think we should switch to normal Python semantics. As @robertwb said, in the same way as we support the comparison methods now.
scoder commentedon Mar 27, 2019
See
generate_richcmp_function()inModuleNode.pytogether with theRichcmpSlotinTypeSlots.py.The idea is to define the number slots with a new slot type
BinopSlotthat has two special Python methods as implementation, and then generate thenb_addetc. slots by dispatching to these methods (more precisely, their C functions) according to CPython's own logic intypeobject.c.[-]Are the right side magic methods supposed to work?[/-][+]Support special binary methods (`__add__` etc.) as Python does[/+][-]Support special binary methods (`__add__` etc.) as Python does[/-][+]Support special binary methods (__add__ etc.) as Python does[/+][-]Support special binary methods (__add__ etc.) as Python does[/-][+]Support special binary methods ("__add__" etc.) as Python does[/+]scoder commentedon Apr 30, 2019
Marking as Cython 3.0 release blocker, in case we need backwards incompatible changes for this.
Limited API updates and cleanup for #2056. GH-3635)
Revert "Limited API updates and cleanup for #2056. GH-3635)"
scoder commentedon Jun 17, 2020
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?
Change the default of the "c_api_binop_methods" directive to False. (G…
robertwb commentedon Jul 2, 2020
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.)
scoder commentedon Jul 2, 2020
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).What do you find still missing?
robertwb commentedon Jul 3, 2020
That's a good idea.
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__.Update the documentation on the arithmetic special methods and issue …
scoder commentedon Jul 9, 2020
I added a warning in ec99487 and also updated the documentation.
scoder commentedon Jul 9, 2020
BTW, I think we do not assign the type of the
selfargument correctly yet, do we? That seems not so easy to change based on a directive, since the slot signatures are built statically.update (#1)