Repository navigation
Use cattrs hooks for nested recursive classes in msgspec - #803
Merged
Merged
Conversation
Merging this PR will not alter performance1 note
Comparing Footnotes |
Member
|
Pretty sure the performance regressions are unrelated. Can you add a link to the PR to the history entry? Also I don't think this is the end of the story - for example, |
The msgspec attrs hook factory generated unstructuring hooks while its recursion probe was still active, so handlers for fields leading back to a class being probed were baked in as msgspec.to_builtins. Hook generation is now deferred to the first call whenever a probe is active, so every level of a recursive class goes through the same cattrs hook.
Tinche
force-pushed
the
fix/msgspec-recursive-hooks
branch
from
October 21, 2026 21:51
efc7b76 to
a0ff0f1
Compare
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #795
With the msgspec converter,
msgspec_attrs_unstructure_factorygenerated a class's unstructuring hook while its recursion probe still held classes in the working set. Field handlers that led back to a probed class were looked up during that time and came back asmsgspec.to_builtins, so nested instances skipped registered hooks and lost private attributes. I changed both generation points (the private-attribute path and the one after the probe) so that, while any probe is active, the factory returns a hook that generates the real one on its first call, after the outermost probe has finished. Outside a probe the hook is generated immediately as before. Returningconverter.unstructureinstead is not enough: a namedtuple field caches the hooks of its element types during the probe, and a private class reached that way would then dispatch to itself forever.The new tests in
tests/preconf/test_msgspec_cpython.pycover a self-recursive class with a hookeddatetimefield, a mutually recursive pair where one class has a private attribute, and a private class reached through a namedtuple field. The first two fail on main with the reported output.