Support for `dataclass`es
See original GitHub issueGiven the close similarity between msgspec.Struct
and dataclasses.dataclass
, am curious to what extent dataclass
es can be used with msgspec
and what this entails.
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
dataclasses — Data Classes — Python 3.11.1 documentation
Source code: Lib/dataclasses.py This module provides a decorator and functions for automatically adding generated special method s such as__init__() ...
Read more >Data Classes in Python 3.7+ (Guide)
Data classes are one of the new features of Python 3.7. ... So far, we have not made a big fuss of the...
Read more >Support dataclasses in TorchScript · Issue #72901 - GitHub
It seems like there are two possible ways to address this problem. First, we could add a new dataclass-like decorator, perhaps torch.
Read more >Why don't Python 3.7 dataclasses support < > <= and >=, or do ...
For version 3.7.1 of the Transcrypt Python to JavaScript compiler I am currently using the new @dataclass ...
Read more >Using Dataclasses - FastAPI
Using Dataclasses¶. FastAPI is built on top of Pydantic, and I have been showing you how to use Pydantic models to declare requests...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the ping (and the nice benchmark plot). I’ve actually been revisiting this opinion, and I think you’ve convinced me that adding simple support for dataclasses is worth it. Now that we have support for
TypedDict
andNamedTuple
objects, asking for dataclass support isn’t that far off. And compatibility withorjson
is a convincing use case. Users really should usemsgspec.Struct
objects instead when possible (they’re much faster, and have fewer weird edge cases than dataclasses), but we can do a decent job with encoding/decoding dataclasses too.I spent some time this evening experimenting with an implementation, and I’m pretty happy with the results. Encoding time is already much faster than what orjson provides, especially when
slots=True
is set:I have JSON encoding done already. Decoding will take a bit more work, but I’d estimate done in the next week or two. I’ll reopen this for now as a reminder.
Ok, #218 is in -
msgspec
now supports encoding/decoding dataclasses. This came together a lot quicker than I expected, and was pretty fun to work on. They’re not as performant or featureful asStruct
types, but perforamance isn’t bad (especially for encoding).I have a few other small fixups I’d like to get in, I’d expect a release sometime next week.