question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement @dataclass for cdef classes

See original GitHub issue

PEP 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:closed
  • Created 4 years ago
  • Reactions:58
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
scodercommented, Jan 29, 2022

cdef dataclasses have landed. Happy to get feedback.

3reactions
da-woodscommented, Jun 2, 2021

What is the status of this?

The status is basically:

  • Regular (i.e. non-cdef) dataclasses should work in the latest alpha release, although there’s probably a few bugs you may run into if using dataclasses.Field.
  • There’s a workable PR that implements 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.
  • The exact implementation of the PR may change, but the features probably won’t (because they’re largely fixed by the need to copy Python). Therefore any code you write to work with the PR will probably work will whatever is released.
  • The PR needs further review before it can be merged. Unfortunately it’s quite a large change (and some of the code may be “mixed” quality) which makes it quite a slow process to review.
  • Hopefully it might make it in the next alpha release (or the one after that). But nobody would like to make promises to give dates.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Extension Types — Cython 3.0.0a11 documentation
Cython supports extension types that behave like the dataclasses defined in the Python 3.7+ standard library. The main benefit of using a dataclass...
Read more >
Does cython support dataclasses or something similar
I asked Cython developers and they opened a feature request for implementing @dataclass for extension types (cdef classes): ...
Read more >
dataclasses — Data Classes — Python 3.11.1 documentation
This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes. It ...
Read more >
Having fun with dataclasses and abstract base classes
Now it's time to create a class that implements the abstract class. As it is described in the reference, for inheritance in dataclasses...
Read more >
transonic.dataclass for numba.jitclass and cython cdef class
transonic.dataclass for numba.jitclass and cython cdef class ; from transonic import dataclass, boost @dataclass class MyStruc: ; attr: int def ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found