Skip to content

Commit 7bc5b7a

Browse files
committed
fix: raise TypeError for unsupported partition source type
The singledispatch fallback of _to_partition_representation used return TypeError(...) instead of raise TypeError(...). For an unsupported (non-primitive) partition source type this returned the exception instance as the partition value instead of raising, so the error would surface far from its cause. Raise it, and add a regression test covering the base handler. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
1 parent 1a87aa3 commit 7bc5b7a

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

pyiceberg/partitioning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def _to_partition_representation(type: IcebergType, value: Any) -> Any:
500500
can return date that still needs to be transformed into an int (days
501501
since epoch).
502502
"""
503-
return TypeError(f"Unsupported partition field type: {type}")
503+
raise TypeError(f"Unsupported partition field type: {type}")
504504

505505

506506
@_to_partition_representation.register(TimestampType)

tests/table/test_partitioning.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
import pytest
2323

2424
from pyiceberg.exceptions import ValidationError
25-
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec
25+
from pyiceberg.partitioning import (
26+
UNPARTITIONED_PARTITION_SPEC,
27+
PartitionField,
28+
PartitionSpec,
29+
_to_partition_representation,
30+
)
2631
from pyiceberg.schema import Schema
2732
from pyiceberg.transforms import (
2833
BucketTransform,
@@ -253,6 +258,13 @@ def test_transform_consistency_with_pyarrow_transform(source_type: PrimitiveType
253258
assert t.transform(source_type)(value) == t.pyarrow_transform(source_type)(pa.array([value])).to_pylist()[0]
254259

255260

261+
def test_to_partition_representation_unsupported_type() -> None:
262+
unsupported_type = StructType(NestedField(1, "x", StringType()))
263+
264+
with pytest.raises(TypeError, match="Unsupported partition field type: struct"):
265+
_to_partition_representation(unsupported_type, "value")
266+
267+
256268
def test_deserialize_partition_field_v2() -> None:
257269
json_partition_spec = """{"source-id": 1, "field-id": 1000, "transform": "truncate[19]", "name": "str_truncate"}"""
258270

0 commit comments

Comments
 (0)