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

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

Support encoding any Enum value type #680

nhairs opened this issue May 7, 2024 · 0 comments

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

No branches or pull requests

1 participant