Skip to content

Commit bb091bd

Browse files
committed
Make Not expression JSON serializable
1 parent 28c2165 commit bb091bd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pyiceberg/expressions/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
Union,
3333
)
3434

35-
from pydantic import model_validator
35+
from pydantic import Field, model_validator
3636

3737
from pyiceberg.expressions.literals import (
3838
AboveMax,

tests/expressions/test_expressions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,29 @@ def test_not() -> None:
736736
assert not_ == pickle.loads(pickle.dumps(not_))
737737

738738

739+
def test_not_json_serialization_and_deserialization() -> None:
740+
expr = Not(AlwaysFalse())
741+
json_str = expr.model_dump_json()
742+
restored = Not.model_validate_json(json_str)
743+
assert isinstance(restored, AlwaysTrue)
744+
745+
expr2 = Not(Not(AlwaysFalse()))
746+
json_str2 = expr2.model_dump_json()
747+
restored2 = Not.model_validate_json(json_str2)
748+
assert isinstance(restored2, AlwaysFalse)
749+
750+
class DummyExpr(BooleanExpression):
751+
def __invert__(self) -> BooleanExpression:
752+
return self
753+
754+
dummy = DummyExpr()
755+
not_dummy = Not(child=dummy)
756+
json_str3 = not_dummy.model_dump_json()
757+
restored3 = Not.model_validate_json(json_str3)
758+
assert isinstance(restored3, Not)
759+
assert isinstance(restored3.child, DummyExpr)
760+
761+
739762
def test_always_true() -> None:
740763
always_true = AlwaysTrue()
741764
assert str(always_true) == "AlwaysTrue()"

0 commit comments

Comments
 (0)