Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nhairs opened this issue May 7, 2024 · 0 comments
Open

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

nhairs opened this issue May 7, 2024 · 0 comments

Comments

@nhairs
Copy link

nhairs commented May 7, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant