Fokko commented on code in PR #2593:
URL: https://github.com/apache/iceberg-python/pull/2593#discussion_r2449286167
##########
pyiceberg/expressions/__init__.py:
##########
@@ -341,21 +343,27 @@ def __getnewargs__(self) -> Tuple[BooleanExpression,
BooleanExpression]:
return (self.left, self.right)
-class Not(BooleanExpression):
+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
Review Comment:
I think we still need the `__new__` method, how about:
```python
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
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]