Clarify msgspec.Raw unsupported with yaml/toml decode by Brohammad · Pull Request #1170 · msgspec/msgspec · GitHub
Skip to content

Clarify msgspec.Raw unsupported with yaml/toml decode - #1170

Open
Brohammad wants to merge 3 commits into
msgspec:mainfrom
Brohammad:fix-raw-yaml-decode-error
Open

Clarify msgspec.Raw unsupported with yaml/toml decode#1170
Brohammad wants to merge 3 commits into
msgspec:mainfrom
Brohammad:fix-raw-yaml-decode-error

Conversation

@Brohammad

Copy link
Copy Markdown

Summary

  • Raise a clear ValidationError when convert targets msgspec.Raw but receives an already-parsed Python object (as happens with yaml.decode / toml.decode)
  • Document that delayed decoding with msgspec.Raw requires the native json or msgpack decoders

Fixes #1136

Root cause

msgspec.Raw requires substrings of the original serialized bytes. The native json and msgpack decoders capture these via *_decode_raw(). The yaml and toml modules parse messages fully with PyYAML/tomllib before calling convert, so the original byte spans are unavailable.

Previously, convert dispatched on the source Python type and produced a confusing error like Expected \any`, got `int``.

Test plan

  • Added test_raw_from_parsed_object_error in test_convert.py
  • Added test_decode_raw_field_unsupported in test_yaml.py
  • Existing Raw tests for json/msgpack/convert still pass (805 tests)

When convert targets msgspec.Raw but the input is an already-parsed
Python object (as with yaml/toml decode), raise a clear ValidationError
instead of a confusing "Expected `any`, got `int`" message. Document
that delayed decoding with Raw requires json or msgpack.

Fixes msgspec#1136

This comment was marked as low quality.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Brohammad

Copy link
Copy Markdown
Author

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.

msgspec.Raw appears not to work with msgspec.yaml.decode

2 participants