Skip to content

Commit 28c2165

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

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

pyiceberg/expressions/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@
3232
Union,
3333
)
3434

35+
from pydantic import model_validator
36+
3537
from pyiceberg.expressions.literals import (
3638
AboveMax,
3739
BelowMin,
3840
Literal,
3941
literal,
4042
)
4143
from pyiceberg.schema import Accessor, Schema
42-
from pyiceberg.typedef import L, StructProtocol
44+
from pyiceberg.typedef import IcebergBaseModel, L, StructProtocol
4345
from pyiceberg.types import DoubleType, FloatType, NestedField
4446
from pyiceberg.utils.singleton import Singleton
4547

@@ -329,21 +331,27 @@ def __getnewargs__(self) -> Tuple[BooleanExpression, BooleanExpression]:
329331
return (self.left, self.right)
330332

331333

332-
class Not(BooleanExpression):
334+
class Not(IcebergBaseModel, BooleanExpression):
333335
"""NOT operation expression - logical negation."""
334336

335337
child: BooleanExpression
336338

337-
def __new__(cls, child: BooleanExpression) -> BooleanExpression: # type: ignore
339+
@model_validator(mode="before")
340+
def _before(cls, values: Any) -> Any:
341+
if isinstance(values, BooleanExpression):
342+
return {"child": values}
343+
return values
344+
345+
@model_validator(mode="after")
346+
def _normalize(cls, model: Any) -> Any:
347+
child = model.child
338348
if child is AlwaysTrue():
339349
return AlwaysFalse()
340350
elif child is AlwaysFalse():
341351
return AlwaysTrue()
342352
elif isinstance(child, Not):
343353
return child.child
344-
obj = super().__new__(cls)
345-
obj.child = child
346-
return obj
354+
return model
347355

348356
def __repr__(self) -> str:
349357
"""Return the string representation of the Not class."""

0 commit comments

Comments
 (0)