Skip to content

Support Literal[True] and Literal[False] types#1004

Open
Siyet wants to merge 6 commits intomainfrom
859-literal-bool
Open

Support Literal[True] and Literal[False] types#1004
Siyet wants to merge 6 commits intomainfrom
859-literal-bool

Conversation

@Siyet
Copy link
Copy Markdown
Collaborator

@Siyet Siyet commented Apr 9, 2026

Reopening #996 (auto-closed when fork was deleted).

Summary

  • Add support for Literal[True], Literal[False], and Literal[True, False] in type definitions
  • Introduce MS_TYPE_BOOLLITERAL_TRUE / MS_TYPE_BOOLLITERAL_FALSE flags to distinguish bool literals from plain bool
  • Handle PyBool_Type before PyLong_Type in typenode_collect_literal (since bool is a subclass of int)
  • Update decode paths (JSON, msgpack, convert) to validate against specific bool literal values
  • Update inspect.py LiteralType.values type annotation to include bool

Closes #859

Add MS_TYPE_BOOLLITERAL_TRUE/FALSE flags and handle PyBool_Type
before PyLong_Type in typenode_collect_literal. Update decode paths
(JSON, msgpack, convert) to validate against specific bool literal
values.

Closes #859
Copy link
Copy Markdown
Collaborator

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

@Siyet
Copy link
Copy Markdown
Collaborator Author

Siyet commented Apr 10, 2026

Two follow-ups pushed:

  • Docs (docs/supported-types.rst) - added bool to the list of supported Literal value types, plus a couple of examples.

  • Error messages (src/msgspec/_core.c) - while testing the doc examples I noticed validation errors for Literal[True] / Literal[False] produced messages with an empty type name (Expected '', got bool). Fixed all three decode paths (mpack_decode_bool, json_decode_true/false, convert_bool) to emit Invalid enum value <val>, consistent with how int/str literals are reported. Test added in test_literal_bool_error_message.

Side note: the Invalid enum value wording is misleading for Literal types in general since they are not enums. I opened #1009 to track that and submitted #1010 (based on this branch) to fix it across all Literal kinds at once.

@Siyet Siyet requested review from jcrist and ofek April 10, 2026 07:32
@Siyet
Copy link
Copy Markdown
Collaborator Author

Siyet commented Apr 10, 2026

The failing build job here is unrelated to this PR: it's the link checker tripping on the Pydantic docs redirect (docs.pydantic.dev/latest/pydantic.dev/docs/validation/...), which is fixed in #1008. Once #1008 lands and this branch is rebased, CI should go green.

@Siyet Siyet self-assigned this Apr 12, 2026
aleksandr.tseluyko added 2 commits April 12, 2026 23:07
Include MS_TYPE_BOOLLITERAL_TRUE/FALSE in the bool check so error
messages like 'Expected ``, got `int`' correctly show 'bool'.
Cover json_decode_true/false and convert_bool with Literal[True],
Literal[False], mixed types, and error message assertions including
the typenode_simple_repr fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Literal[bool]

2 participants