Skip to content

Extend dict keys to allow for string literals? #76

Description

@CamDavidsonPilon

Currently, string literals can't be used as dict keys in a Struct. See example below:

from typing import Literal
from msgspec import Struct, json

AorB = Literal["a", "b"]

class Test(Struct):
    d: dict[AorB, float]

data = Test(d={'a': 1.0, 'b': 2.0})
encoded = json.encode(data)
print(encoded)

decoded = json.decode(encoded, type=Test) # TypeError: JSON doesn't support dicts with non-string keys - type `dict[typing.Literal['a', 'b'], float]` is not supported

I would think it should be allowed - but I'd like to hear your thoughts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions