Skip to content

Validation errors for Literal types incorrectly say 'enum value' #1009

Description

@Siyet

Description

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

import msgspec
from typing import Literal

msgspec.json.decode(b'4', type=Literal[1, 2, 3])
# msgspec.ValidationError: Invalid enum value 4

msgspec.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).
  • Bool literals (added in Support Literal[True] and Literal[False] types #1004) use a separate code path but produce the same misleading wording.

Activity

  1. added a commit that references this issue on Apr 10, 2026
    b437602
  2. added a commit that references this issue on Apr 20, 2026
    847c304
  3. Brohammad commented on Nov 7, 2026

    @Brohammad

    Opened a PR that distinguishes Literal from Enum in shared lookup error paths: enums still say Invalid enum value, literals say Invalid value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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