Fix deepcopy for And, Or, and Not expressions#3295
Open
robreeves wants to merge 6 commits intoapache:mainfrom
Open
Fix deepcopy for And, Or, and Not expressions#3295robreeves wants to merge 6 commits intoapache:mainfrom
robreeves wants to merge 6 commits intoapache:mainfrom
Conversation
Pydantic v2's BaseModel.__deepcopy__ calls cls.__new__(cls) with no args, but And, Or, and Not define __new__ with required positional arguments. These tests reproduce the TypeError on deepcopy.
Add __deepcopy__ to BooleanExpression that reconstructs the expression via model_fields, and returns self for Singleton subclasses. Fixes TypeError from Pydantic v2's BaseModel.__deepcopy__ calling cls.__new__(cls) with no args on And, Or, and Not which require positional arguments in __new__.
Instead of a generic __deepcopy__ on BooleanExpression with Pydantic internals, add simple __deepcopy__ methods to the three classes that need them. Also add identity assertions for non-Singleton copies.
75aab0e to
e992078
Compare
rambleraptor
approved these changes
Apr 29, 2026
Contributor
rambleraptor
left a comment
There was a problem hiding this comment.
Seems pretty straight forward. Thanks for doing this!
ndrluis
approved these changes
Apr 30, 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.
Closes #3297
Rationale for this change
copy.deepcopy()onAnd,Or, andNotexpressions raisesTypeErrorbecause Pydantic v2's default__deepcopy__callscls.__new__(cls)with no args, but these classes require positional arguments in__new__.The fix adds
__deepcopy__toAnd,Or, andNotthat deepcopy their fields and call the constructor directly.Are these changes tested?
Yes. 11 new unit tests covering all expression types, balanced trees, nested expressions, and deepcopy followed by pickle.
Are there any user-facing changes?
Yes,
copy.deepcopy()on expressions now works instead of raising an exception.