Skip to content

[BUG] __add__ method gets Python object not cython object as self. #4434

@gentlegiantJGC

Description

@gentlegiantJGC

Describe the bug
This may be working as intended if so I will have to find a workaround for it but I just wanted to confirm first.
The __eq__ method of a class in cython is given the cython version of the class even if called from python.
I would expect this behaviour to apply to all dunder methods but it does not apply to the __add__ and __radd__ methods.
These two methods are given the python object from which the internal attributes cannot be accessed.
This may apply to more methods but the add methods are the only ones I have currently tested.

To Reproduce
Code to reproduce the behaviour:

from cython import typeof

cdef class Test:
    def __eq__(self, other):
        print("eq", typeof(self), repr(self))
        return [] == other

    def __add__(self, other):
        print("add", typeof(self), repr(self))
        return [] + other

    def __radd__(self, other):
        print("radd", typeof(self), repr(self))
        return other + []

cdef Test test = Test()
a = test == []  # eq Test <test.Test object at 0x000001267FF376C0>
b = test + []  # add Python object <test.Test object at 0x000001F97E77C6C0>
c = [] + test  # radd Python object <test.Test object at 0x000001C24B3966C0>

Expected behavior
I would expect the add methods to be given the cython version of Test so that the internal attributes can be accessed.

Environment (please complete the following information):

  • OS: Windows
  • Python version 3.9.1
  • Cython version 3.0.0a9

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions