Implement @dataclass for cdef classes
See original GitHub issuePEP 557 defines dataclasses, a new feature in Py3.7. It would be nice if Cython could generate equivalent code for cdef classes directly at translation time when it finds an @dataclass
constructor on them.
This can be done by generating Cython code and injecting it into the module, similar to what we do for fused types (see FusedNode.py
) or auto-pickling (see the AnalyseDeclarationsTransform
in ParserTreeTransforms.py
). The latter would probably also be a good place to implement the @dataclass
decorator, although it seems worth keeping the main part of the implementation in a separate module instead of the transform itself, e.g. Cython.Compiler.Dataclasses
.
Since this only applies to cdef classes, it seems better to use a safe and dedicated separate decorator @cython.dataclass
than to rely on the generic Python decorator, at least initially.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:58
- Comments:7 (1 by maintainers)
Top GitHub Comments
cdef dataclasses have landed. Happy to get feedback.
The status is basically:
dataclasses.Field
.cdef class
dataclasses (i.e. this issue). If you’re feeling adventurous you can check out that code and use it yourself right now. Any feedback you have from trying to use it would be useful.