Dataclasses do not have a serialisation method
See original GitHub issueFeature Request
For bugs/questions:
- OS: ubuntu 18.10
- Python version
import sys; print(sys.version)
: 3.7 - Pydantic version
import pydantic; print(pydantic.VERSION)
: from github master: 2fb0b26b81cab30641dc7d780021ee762ba1b076
from datetime import datetime
from pydantic.dataclasses import dataclass
@dataclass
class User:
id: int
name: str = 'John Doe'
signup_ts: datetime = None
user = User(id='42', signup_ts='2032-06-21T12:00+00:00')
print(user)
print(user.dict())
(py37) 06:33:45 {master} ~/Downloads/pydantic$ python /tmp/test.py
User(id=42, name='John Doe', signup_ts=datetime.datetime(2032, 6, 21, 12, 0, tzinfo=datetime.timezone.utc))
Traceback (most recent call last):
File "/tmp/test.py", line 14, in <module>
print(user.dict())
AttributeError: 'User' object has no attribute 'dict'
It is not clear how to serialise an object created as @dataclass
It works fine if I use the pydantic.BaseModel
subclass style, though.
Also, related question, I’m not sure what is the advantage of one style over the other. It’s just confusing to have two ways of doing the same thing (especially if one of them is slightly broken).
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Dataclasses do not have a serialisation method #365 - GitHub
It is not clear how to serialise an object created as @dataclass It works fine if I use the pydantic.BaseModel subclass style, though....
Read more >dataclasses-serialization - PyPI
Fields are deserialized using the type provided by the dataclass. So bound generic dataclasses may be deserialized, while unbound ones may not.
Read more >Serialize dataclass with class as a field to JSON in Python
I'm contributing new dataclasses (like NormalDataclass ) to this project and I need to be able to serialize them to JSON. I don't...
Read more >Everything you need to know about dataclasses - rmcomplexity
Data classes also make it easier to create frozen (immutable) instances, serialize instances and enforce type hints usage. The main parts of a ......
Read more >Serialization - Dataclasses Avro Schema - Marcos Schroh
Is possible to serialize/deserialize with the correspondent avro schema generated and the dataclass. In both cases we can do it with avro or...
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 Free
Top 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
If someone is still looking for this, the dataclasses module offers dc2dict in the form of
asdict
:Maybe updating the docs on this issue would help clarify. Currently the docs read:
Updating this to be more explicit would be helpful: