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.

Dataclasses do not have a serialisation method

See original GitHub issue

Feature 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
eelkevdboscommented, Apr 8, 2020

If someone is still looking for this, the dataclasses module offers dc2dict in the form of asdict:

from dataclasses import dataclass, asdict

@dataclass
class InventoryItem:
    """Class for keeping track of an item in inventory."""
    name: str
    unit_price: float
    quantity_on_hand: int = 0

asdict(InventoryItem(name="A", unit_price=1.0))
#> {"name": "A", "unit_price": 1.0, "quantity_on_hand": 0}
5reactions
JakeSummerscommented, Mar 5, 2019

Maybe updating the docs on this issue would help clarify. Currently the docs read:

You can use all the standard pydantic field types and the resulting dataclass will be identical to the one created by the standard library dataclass decorator.

Updating this to be more explicit would be helpful:

You can use all the standard pydantic field types and the resulting dataclass will be identical to the one created by the standard library dataclass decorator. The resulting object will NOT have any pydantic serialization methods.

Read more comments on GitHub >

github_iconTop 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 >

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