From 7bc5b7a4d4818b4691243f7d42321e860962d133 Mon Sep 17 00:00:00 2001 From: Anas Khan <83116240+anxkhn@users.noreply.github.com> Date: Sat, 4 Jul 2026 15:49:16 +0530 Subject: [PATCH] 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> --- pyiceberg/partitioning.py | 2 +- tests/table/test_partitioning.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pyiceberg/partitioning.py b/pyiceberg/partitioning.py index 3074c30ea1..4279ff5f09 100644 --- a/pyiceberg/partitioning.py +++ b/pyiceberg/partitioning.py @@ -500,7 +500,7 @@ def _to_partition_representation(type: IcebergType, value: Any) -> Any: can return date that still needs to be transformed into an int (days since epoch). """ - return TypeError(f"Unsupported partition field type: {type}") + raise TypeError(f"Unsupported partition field type: {type}") @_to_partition_representation.register(TimestampType) diff --git a/tests/table/test_partitioning.py b/tests/table/test_partitioning.py index b150fc2f67..18181ee5ca 100644 --- a/tests/table/test_partitioning.py +++ b/tests/table/test_partitioning.py @@ -22,7 +22,12 @@ import pytest from pyiceberg.exceptions import ValidationError -from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec +from pyiceberg.partitioning import ( + UNPARTITIONED_PARTITION_SPEC, + PartitionField, + PartitionSpec, + _to_partition_representation, +) from pyiceberg.schema import Schema from pyiceberg.transforms import ( BucketTransform, @@ -253,6 +258,13 @@ def test_transform_consistency_with_pyarrow_transform(source_type: PrimitiveType assert t.transform(source_type)(value) == t.pyarrow_transform(source_type)(pa.array([value])).to_pylist()[0] +def test_to_partition_representation_unsupported_type() -> None: + unsupported_type = StructType(NestedField(1, "x", StringType())) + + with pytest.raises(TypeError, match="Unsupported partition field type: struct"): + _to_partition_representation(unsupported_type, "value") + + def test_deserialize_partition_field_v2() -> None: json_partition_spec = """{"source-id": 1, "field-id": 1000, "transform": "truncate[19]", "name": "str_truncate"}"""