Skip to content

Support Optional[Raw] when decoding JSON and MessagePack - #1155

Closed
DSeaStar wants to merge 3 commits into
msgspec:mainfrom
DSeaStar:fix-optional-raw
Closed

DSeaStar wants to merge 3 commits into
msgspec:mainfrom
DSeaStar:fix-optional-raw

Conversation

@DSeaStar

@DSeaStar DSeaStar commented Aug 14, 2026 •

Copy link
Copy Markdown

Fixes #659.

Support Raw | None (Optional[Raw]) when decoding JSON and MessagePack. JSON null and MessagePack nil decode as None; other values are captured as Raw. Previously, non-null values failed validation for this annotation.

Reject unions combining Raw with other concrete types, while preserving normal Any normalization. Add conversion, diagnostic and typing coverage, and document null handling and union restrictions.

Raw was stored with a type mask of 0, so Optional[Raw] never recorded
MS_TYPE_NONE and json/msgpack decode treated null as a Raw payload.
Give Raw its own type flag, allow it only in a union with None, and
decode null as None while leaving other values as Raw.

@Siyet Siyet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thx for working on this! Pls rebase on main and preserve the raw diagnostic from #1169, including both raw and null for Raw | None. Also document null handling and supported unions, and add a nullable Raw example to the typing checks.

Siyet added 2 commits November 9, 2026 18:51
Preserve Raw and nullable Raw validation diagnostics when resolving conflicts.
@Siyet
Siyet requested a review from sobolevn November 9, 2026 18:28

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think that this should be ever done. Why None is a special value? Why not other unions?

Raw allows you to parse any data for later parsing. Later you can parse None or any other objects and handle them.

@sobolevn sobolevn closed this Nov 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.

Optional[Raw] results in an unexpected validation error at json.decode time

3 participants