msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

Json Encoder: dataclass classes are encoded to empty dict

Open nhairs opened this issue 1 year ago • 0 comments
trafficstars

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

nhairs avatar May 07 '24 09:05 nhairs