Repository navigation
Support namedtuple and tuple subclasses in tree_merge - #3703
Merged
angeloskath merged 1 commit intoJun 17, 2026
Merged
Conversation
tree_map and tree_map_with_path reconstruct list/tuple containers with `TreeType(*subtrees) if hasattr(tree, "_fields") else TreeType(subtrees)` so that namedtuples (which take one positional arg per field) and tuple subclasses round-trip correctly. tree_merge still used the plain `TreeType(<generator>)` form, so merging two namedtuple pytrees bound the whole generator to the first field and raised a misleading TypeError about the merge function's arguments. Mirror the same reconstruction idiom in tree_merge and extend test_supported_trees to cover tree_merge over a namedtuple and a tuple subclass.
angeloskath
approved these changes
Jun 16, 2026
angeloskath
left a comment
Member
There was a problem hiding this comment.
Looks good to me, thanks!
tpegolotti
pushed a commit
that referenced
this pull request
Jun 22, 2026
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.
Summary
tree_mapandtree_map_with_pathreconstructlist/tuplecontainers withso that namedtuples (whose constructor takes one positional argument per field) and tuple subclasses round-trip correctly.
tree_mergewas still using the plainTreeType(<generator>)form, so merging two namedtuple pytrees binds the whole generator to the first field and raises a misleadingTypeErrorpointing at the merge function:This applies the same reconstruction idiom already used by
tree_map/tree_map_with_pathso namedtuples and tuple subclasses merge correctly, while plain lists and tuples are unchanged.Changes
tree_mergereconstructs_fields-bearing containers withTreeType(*subtrees).test_supported_treesto covertree_mergeover a namedtuple and atuplesubclass.Result