Closed
Description
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.