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 bug: Encoding includes unspecified instance attributes

See original GitHub issue

When encoding a dataclass, instance attributes that aren’t dataclass-fields are included in the result:

import dataclasses
import msgspec


@dataclasses.dataclass
class Foo:
    id: int


foo = Foo(id=1)
foo.bar = "test"


print(msgspec.json.encode(foo))
print(msgspec.json.encode(dataclasses.asdict(foo)))

assert msgspec.json.encode(foo) == msgspec.json.encode(dataclasses.asdict(foo))

This is problematic because it differs from dataclasses.asdict, and in turn makes mgspec incompatible with pydantic.dataclasses:

from dataclasses import asdict

import msgspec
from pydantic.dataclasses import dataclass


@dataclass
class Foo:
    id: int


foo = Foo(id=1)


print(msgspec.json.encode(foo))
print(msgspec.json.encode(asdict(foo)))

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
provinzkrautcommented, Dec 8, 2022

Mmk. Should be a quick fix; expect a patch release in the next day or two.

Perfect, thanks a lot!

If you have time, could you check if there are any other incompatibilities so we can batch them all in one release?

I just ran our test suites against the latest release and this was the only thing that popped up. Nothing else comes to mind at the moment, but should I come across anything else I’ll let you know!

1reaction
jcristcommented, Dec 8, 2022

Mmk. Should be a quick fix; expect a patch release in the next day or two. If you have time, could you check if there are any other incompatibilities so we can batch them all in one release?

Read more comments on GitHub >

github_iconTop Results From Across the Web

lidatong/dataclasses-json: Easily serialize Data ... - GitHub
This library provides a simple API for encoding and decoding dataclasses to and from JSON. It's very easy to get started. README /...
Read more >
python - Dataclass attribute missing when using explicit __ ...
I wanted field() to provide a default value for the field whether or not I wrote my own custom constructor. One of the...
Read more >
Everything you need to know about dataclasses - rmcomplexity
Updating an object instance by replacing the entire object. Adding class attributes; Inheritance in data classes; Hash-able object; A use case ...
Read more >
Python - dataclasses — Data Classes | Docs4dev
It is an error for changes to contain any fields that are defined as having init=False . A ValueError will be raised in...
Read more >
too-many-instance-attributes / R0902 - Pylint
Used when class has too many instance attributes, try to reduce this to get a simpler (and so easier to use) class. Problematic...
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