Skip to content

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

Description

@nhairs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions