Make Not expression JSON serializable#2593
Merged
Fokko merged 9 commits intoapache:mainfrom Oct 23, 2025
Merged
Conversation
rambleraptor
reviewed
Oct 9, 2025
Contributor
rambleraptor
left a comment
There was a problem hiding this comment.
Can you write a test for:
- Converting a Not Expression to JSON
- Converting a JSON Not Expression -> Python Not Expression
This does just enough Pydantic magic that I'm concerned about potential side-effects and a test will help ground us.
ee4fac1 to
bb091bd
Compare
Contributor
Author
|
@rambleraptor I have updated with changes, please let me know if this needs improvement. Thanks! |
Contributor
Author
|
@kevinjqliu could you please review these changes, not sure ci check fails are related to this. |
Fokko
reviewed
Oct 21, 2025
Fokko
reviewed
Oct 21, 2025
pyiceberg/expressions/__init__.py
Outdated
Comment on lines
346
to
366
| class Not(IcebergBaseModel, BooleanExpression): | ||
| """NOT operation expression - logical negation.""" | ||
|
|
||
| child: BooleanExpression | ||
|
|
||
| def __new__(cls, child: BooleanExpression) -> BooleanExpression: # type: ignore | ||
| @model_validator(mode="before") | ||
| def _before(cls, values: Any) -> Any: | ||
| if isinstance(values, BooleanExpression): | ||
| return {"child": values} | ||
| return values | ||
|
|
||
| @model_validator(mode="after") | ||
| def _normalize(cls, model: Any) -> Any: | ||
| child = model.child | ||
| if child is AlwaysTrue(): | ||
| return AlwaysFalse() | ||
| elif child is AlwaysFalse(): | ||
| return AlwaysTrue() | ||
| elif isinstance(child, Not): | ||
| return child.child | ||
| obj = super().__new__(cls) | ||
| obj.child = child | ||
| return obj | ||
| return model |
Contributor
There was a problem hiding this comment.
I think we still need the __new__ method, how about:
class Not(IcebergBaseModel, BooleanExpression):
"""NOT operation expression - logical negation."""
model_config = ConfigDict(arbitrary_types_allowed=True)
type: TypingLiteral["not"] = Field(default="not")
child: BooleanExpression = Field()
def __init__(self, child: BooleanExpression, **_) -> None:
super().__init__(child=child)
def __new__(cls, child: BooleanExpression, **_) -> BooleanExpression: # type: ignore
if child is AlwaysTrue():
return AlwaysFalse()
elif child is AlwaysFalse():
return AlwaysTrue()
elif isinstance(child, Not):
return child.child
obj = super().__new__(cls)
return obj
Contributor
Author
There was a problem hiding this comment.
sure, i'll update and test it.
Co-authored-by: Fokko Driesprong <fokko@apache.org>
Fokko
reviewed
Oct 23, 2025
Fokko
reviewed
Oct 23, 2025
Fokko
reviewed
Oct 23, 2025
Fokko
reviewed
Oct 23, 2025
Fokko
reviewed
Oct 23, 2025
Fokko
approved these changes
Oct 23, 2025
Contributor
Fokko
left a comment
There was a problem hiding this comment.
Let's go! Thanks @Aniketsy for working on this, and thanks @rambleraptor for the review 🚀
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.
#2520
This PR makes the
Notboolean expression inpyicebergJSON serializable using PydanticPlease let me know if my approach or fix needs any improvements . I’m open to feedback and happy to make changes based on suggestions.
Thank you !