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

Structs with dictionaries with enum keys do not encode #799

Open
ahrnbom opened this issue Jan 12, 2025 · 2 comments
Open

Structs with dictionaries with enum keys do not encode #799

ahrnbom opened this issue Jan 12, 2025 · 2 comments

Comments

@ahrnbom
Copy link

ahrnbom commented Jan 12, 2025

Description

On msgspec 0.19.0, I get this behaviour:

from typing import Dict
from enum import Enum
import msgspec


class SomeEnum(Enum):
    EXAMPLE_VALUE = "hello"


class SomeModel(msgspec.Struct):
    values: Dict[SomeEnum, int] = {}


def test_msgspec_logic():
    a = SomeModel()
    a.values[SomeEnum.EXAMPLE_VALUE] = 2
    msgspec.json.encode(a)

if __name__ == "__main__":
    test_msgspec_logic()

I get this error:

(venv) abone@fedora:~/mflu/mflu/tests$ python3 test_models.py 
Traceback (most recent call last):
  File "/home/abone/mflu/mflu/tests/test_models.py", line 20, in <module>
    test_msgspec_logic()
    ~~~~~~~~~~~~~~~~~~^^
  File "/home/abone/mflu/mflu/tests/test_models.py", line 17, in test_msgspec_logic
    msgspec.json.encode(a)
    ~~~~~~~~~~~~~~~~~~~^^^
TypeError: Only dicts with str-like or number-like keys are supported

This used to work in 0.18.6 on Python 3.12, but with 0.19.0 on Python 3.13, it does not.

I am currently making a game that relies a lot on this behaviour, so I will gladly help with narrowing the problem down or whatever else I can do.

@ahrnbom
Copy link
Author

ahrnbom commented Jan 12, 2025

I just confirmed that this behaviour is tied to msgspec 0.19.0 itself, running 0.19.0 in Python 3.12 has the same bug.

@jack-mcivor
Copy link

I can also produce in Python 3.11, msgspec==0.19.0 fails while msgspec==0.18.6 passes without issue. Decoding also seems to work fine. Seems like a reasonably significant regression

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

2 participants