jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.01k stars 59 forks source link

Json Encoder: dataclass classes are encoded to empty dict #681

Open nhairs opened 1 month ago

nhairs commented 1 month ago

Question

I've noticed that when encoding a dataclass class (as opposed to an instance), the class is encoded to an empty dict.

I was wondering if this was intentional behaviour? I would have expected an error as if I had passed in any other class (rather than instance).

>>> import msgspec.json
>>> e = msgspec.json.Encoder()
>>> from dataclasses import dataclass
>>> @dataclass
... class SomeDataclass:
...   foo: int
... 
>>> e.encode(SomeDataclass)
b'{}'
>>> class SomeClass:
...  pass
... 
>>> e.encode(SomeClass)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Encoding objects of type type is unsupported