Dataclasses bug: Encoding includes unspecified instance attributes
See original GitHub issueWhen 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:
- Created 9 months ago
- Comments:10 (5 by maintainers)
Top 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 >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
Perfect, thanks a lot!
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!
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?