Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed properties in cdef classes not calling `__get__` #3139

Open
jacklovell opened this issue Sep 9, 2019 · 0 comments

Comments

@jacklovell
Copy link

commented Sep 9, 2019

I have an Extension class with some properties, which I'd like to type for efficient use inside the class. However, adding types to the properties in the class definition means that these properties are not called from cython code, and default initial values for the corresponding types are used instead.

For example, consider the following class:

cdef class Test:
    cdef:
        int a, _a
        double b, _b
        list c
        double d

    def __init__(self):
        self._a = 1
        self._b = 3.14

    @property
    def a(self):
        return self._a

    @property
    def b(self):
        return self._b

    @property
    def c(self):
        return [self._a * self._b for _ in range(5)]

    def get_c(self):
        return self.c

    def calc_c(self):
        return [self.a * self.b for _ in range(5)]

    @property
    def d(self):
        return self.a * self.b

And here is the behaviour it exhibits:

In [1]: import pyximport; pyximport.install()                                                            
Out[1]: (None, <pyximport.pyximport.PyxImporter at 0x7f56b06ff198>)

In [2]: from test_cdef_class import Test                                                                 

In [3]: t = Test()                                                                                       

In [4]: print(t.a, t.b, t.c, sep='\n')  # Properties work when called from python
1
3.14
[3.14, 3.14, 3.14, 3.14, 3.14]

In [5]: print(t.get_c(), t.calc_c(), t.d, sep='\n')  # Properties don't work when called from cython internally
None
[0.0, 0.0, 0.0, 0.0, 0.0]
0.0

I would expect that when getting a property of the class the behaviour would be consistent whether the get is coming from Python, or internally in Cython-generated code.

@jacklovell jacklovell changed the title Typed properties in cdef classes not callinng `__get__` Typed properties in cdef classes not calling `__get__` Sep 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.