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
Clarify whether cdef class attributes must have a cdef #3154
The documentation states that cdef classes (by default, i.e. without an explicit __dict__) have the restriction "All attributes must be pre-declared at compile-time" / "The set of attributes is fixed at compile time".
Does this mean "all attributes used must exist in the class-level source" or "all attributes used must have a cdef in the class-level source"? i.e. is "Maybe" below allowed?
One real-world instance of this is pandas _Timedelta.__array_priority__. This class works most of the time, but this crash bug looks like Python trying to access an instance dict that C didn't allocate space for, which plausibly might be a consequence of it not being allowed.
Please state explicitly whether this is allowed or not.
(It would be even better to either make it a compile-time error or treat it as 'cdef public object', but that might require an unreasonably large amount of work.)
cdef attributes can't have default values (though that at least is a compile-time error and not mystery crashes), so my above "Allowed" actually isn't, and just adding 'cdef public object' to them all doesn't fix pandas. Adding 'cdef dict dict' (in the .pxd if there is one) to enable dynamic attributes probably will, but at a performance cost.
However, my original request to document whether "Maybe:" is allowed remains open.
"Maybe" defines a class attribute (i.e. a single value shared between all instances, rather than one value per instance) - It's allowed but it doesn't do what you think. It's mostly consistent with what would happen if you put