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

Support encoding any Enum value type #680

Closed
nhairs opened this issue May 7, 2024 · 0 comments · Fixed by #757
Closed

Support encoding any Enum value type #680

nhairs opened this issue May 7, 2024 · 0 comments · Fixed by #757

Comments

@nhairs
Copy link

nhairs commented May 7, 2024

Description

Currently msgspec only supports encoding Enum values that strings or integers, throwing msgspec.EncodeError: Only enums with int or str values are supported when attempting to encode anything else.

As far as I can tell this affects the following encoders:

  • msgpack (have not tested, only from reading source code)
  • json (have tested)

It feels like any value supported by the encoder should be encoded rather than just these two specific types.

Alternatively, in the case of the JSON encoder (I haven't checked the other encoders), it should be possible to use the enc_hook to handle the other types. Currently this is not possible as the error is thrown before the enc_hook is called.

The specific use case that I'm hitting this on is in adding support for the msgspec encoder to python-json-logger (nhairs/python-json-logger#12)

>>> import msgspec.json
>>> e = msgspec.json.Encoder()
>>> import enum
>>> class SomeEnum(enum.Enum):
...  NULL = None
... 
>>> e.encode(SomeEnum.NULL)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
msgspec.EncodeError: Only enums with int or str values are supported
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

Successfully merging a pull request may close this issue.

1 participant