Structs with dictionaries with enum keys do not encode
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.1k
- Forks
- 193
- Avg merge
- 3d 20h
- Merged PRs (30d)
- 15
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the supplied Python reproduction with msgspec.json.encode on a Struct containing a dictionary keyed by SomeEnum, comparing the reported 0.18.6 and 0.19.0 behavior where possible. Trace the encoding entry point and relevant dictionary-key handling; done means enum-keyed dictionaries encode successfully without the reported TypeError and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100