Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Typed properties in cdef classes not calling `__get__` #3139
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 : import pyximport; pyximport.install() Out: (None, <pyximport.pyximport.PyxImporter at 0x7f56b06ff198>) In : from test_cdef_class import Test In : t = Test() In : 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 : 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.