You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When validation fails for a typing.Literal type, msgspec raises a ValidationError with the message Invalid enum value <val>. This is misleading because the user is not using enum.Enum, they are using typing.Literal.
Reproduction
importmsgspecfromtypingimportLiteralmsgspec.json.decode(b'4', type=Literal[1, 2, 3])
# msgspec.ValidationError: Invalid enum value 4msgspec.json.decode(b'"bad"', type=Literal["one", "two"])
# msgspec.ValidationError: Invalid enum value 'bad'msgspec.json.decode(b'false', type=Literal[True])
# msgspec.ValidationError: Invalid enum value False
Expected
A wording that does not reference enum, since the type is not an enum. For example:
Invalid value 4
Invalid value 'bad'
Invalid value False
Or, even more useful, a message that lists the allowed literals:
Invalid value 4, expected one of (1, 2, 3)
Notes
This affects all decode paths (JSON, MsgPack, convert).
The same error path (_Lookup_OnMissing) is shared between Enum and Literal, so the fix needs to differentiate between the two cases (when lookup->cls != NULL it is an Enum, otherwise it is a Literal).
Description
When validation fails for a
typing.Literaltype, msgspec raises aValidationErrorwith the messageInvalid enum value <val>. This is misleading because the user is not usingenum.Enum, they are usingtyping.Literal.Reproduction
Expected
A wording that does not reference
enum, since the type is not an enum. For example:Or, even more useful, a message that lists the allowed literals:
Notes
convert)._Lookup_OnMissing) is shared betweenEnumandLiteral, so the fix needs to differentiate between the two cases (whenlookup->cls != NULLit is anEnum, otherwise it is aLiteral).