Skip to content

Commit fcdd773

Browse files
committed
use model_serializer instead
1 parent 7d1c461 commit fcdd773

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

pyiceberg/table/update/__init__.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from functools import singledispatch
2424
from typing import TYPE_CHECKING, Annotated, Any, Dict, Generic, List, Literal, Optional, Tuple, TypeVar, Union, cast
2525

26-
from pydantic import Field, field_serializer, field_validator, model_validator
26+
from pydantic import Field, field_validator, model_validator, model_serializer
2727

2828
from pyiceberg.exceptions import CommitFailedException
2929
from pyiceberg.partitioning import PARTITION_FIELD_ID_START, PartitionSpec
@@ -725,17 +725,15 @@ class AssertRefSnapshotId(ValidatableTableRequirement):
725725

726726
type: Literal["assert-ref-snapshot-id"] = Field(default="assert-ref-snapshot-id")
727727
ref: str = Field(...)
728-
snapshot_id: int = Field(default=-1, alias="snapshot-id")
729-
730-
# serialize -1 to null when serializing to json
731-
# TODO: make more generic Field so this can be used on other models that need
732-
# an explicit null.
733-
@field_serializer("snapshot_id", when_used="json")
734-
def snapshot_id_can_be_null(self, snapshot_id: int) -> Optional[int]:
735-
if snapshot_id == -1:
736-
return None
737-
else:
738-
return snapshot_id
728+
snapshot_id: Optional[int] = Field(default=None, alias="snapshot-id")
729+
730+
@model_serializer
731+
def ser_model(self) -> dict[str, Any]:
732+
return {
733+
"type": self.type,
734+
"ref": self.ref,
735+
"snapshot_id": self.snapshot_id,
736+
}
739737

740738
def validate(self, base_metadata: Optional[TableMetadata]) -> None:
741739
if base_metadata is None:

tests/test_serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_null_serializer_field() -> None:
5656
class ExampleRequest(IcebergBaseModel):
5757
requirements: Tuple[TableRequirement, ...]
5858

59-
request = ExampleRequest(requirements=(AssertRefSnapshotId(ref="main"),))
59+
request = ExampleRequest(requirements=(AssertRefSnapshotId(ref="main", snapshot_id=None),))
6060
dumped_json = request.model_dump_json()
6161
expected_json = """{"type":"assert-ref-snapshot-id","ref":"main","snapshot-id":null}"""
6262
assert expected_json in dumped_json

0 commit comments

Comments
 (0)