Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pyiceberg/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

from pydantic import Field

from pydantic import Field, model_validator

from pyiceberg.expressions.literals import (
AboveMax,
BelowMin,
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

@Aniketsy Aniketsy Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i'll update and test it.


def __repr__(self) -> str:
"""Return the string representation of the Not class."""
Expand Down
23 changes: 23 additions & 0 deletions tests/expressions/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,29 @@ def test_not() -> None:
assert not_ == pickle.loads(pickle.dumps(not_))


def test_not_json_serialization_and_deserialization() -> None:
expr = Not(AlwaysFalse())
json_str = expr.model_dump_json()
restored = Not.model_validate_json(json_str)
assert isinstance(restored, AlwaysTrue)

expr2 = Not(Not(AlwaysFalse()))
json_str2 = expr2.model_dump_json()
restored2 = Not.model_validate_json(json_str2)
assert isinstance(restored2, AlwaysFalse)

class DummyExpr(BooleanExpression):
def __invert__(self) -> BooleanExpression:
return self

dummy = DummyExpr()
not_dummy = Not(child=dummy)
json_str3 = not_dummy.model_dump_json()
restored3 = Not.model_validate_json(json_str3)
assert isinstance(restored3, Not)
assert isinstance(restored3.child, DummyExpr)


def test_always_true() -> None:
always_true = AlwaysTrue()
assert always_true.model_dump_json() == '"true"'
Expand Down
Loading