Skip to content

Structs with dictionaries with enum keys do not encode #799

Open
@ahrnbom

Description

@ahrnbom

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions