Skip to content

Support Literal[True] and Literal[False] types - #996

Closed
Siyet wants to merge 1 commit into
msgspec:mainfrom
Siyet:859-literal-bool
Closed

Siyet wants to merge 1 commit into
msgspec:mainfrom
Siyet:859-literal-bool

Conversation

@Siyet

@Siyet Siyet commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

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

Test plan

Ubuntu 22.04 (WSL2, x86_64, gcc 11.4, Python 3.10):

6128 passed, 70 skipped in 12.25s

Ubuntu 24.04 (WSL2, x86_64, gcc 13.3, Python 3.12):

24 passed in 0.35s (TestLiterals)

New parametrized tests cover:

  • Literal[True] - accepts True, rejects False

  • Literal[False] - accepts False, rejects True

  • Literal[True, False] - accepts both

  • Literal[1, False] - mixed int + bool literal

  • Literal[True, "yes", None] - mixed bool + str + None literal

  • Union[Literal[True], bool] - bool literal combined with plain bool

  • CI passes on all platforms

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
@Siyet

Siyet commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

CI failures are unrelated to this change:

"Profile Windows (ARM64)" - runner infra issue ("Install command runner" step)
"build" (documentation) - link checker fails on a pre-existing broken link (#976)
All build, test, and wheel jobs pass across all platforms.

@Siyet

Siyet commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

Resolved in the community fork msgspec-arise: PR #12, released in v0.20.2.

@Siyet Siyet closed this by deleting the head repository Apr 9, 2026
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]

1 participant