Skip to content

Commit eefd5b4

Browse files
Fix misleading error messages in conversions dispatch fallbacks
1 parent 68898e5 commit eefd5b4

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

‎pyiceberg/conversions.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _(_: PrimitiveType, value_str: str) -> bytes:
198198

199199
@singledispatch
200200
def to_bytes(
201-
primitive_type: PrimitiveType, _: bool | bytes | Decimal | date | datetime | float | int | str | time | uuid.UUID
201+
primitive_type: PrimitiveType, value: bool | bytes | Decimal | date | datetime | float | int | str | time | uuid.UUID
202202
) -> bytes:
203203
"""Convert a built-in python value to bytes.
204204
@@ -208,10 +208,10 @@ def to_bytes(
208208
209209
Args:
210210
primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
211-
_: The value to convert to bytes (The type of this value depends on which dispatched function is
211+
value: The value to convert to bytes (The type of this value depends on which dispatched function is
212212
used--check dispatchable functions for type hints).
213213
"""
214-
raise TypeError(f"scale does not match {primitive_type}")
214+
raise TypeError(f"Cannot serialize to bytes, type {primitive_type} not supported: {value!r}")
215215

216216

217217
@to_bytes.register(BooleanType)
@@ -408,7 +408,7 @@ def to_json(primitive_type: PrimitiveType, val: Any) -> L: # type: ignore
408408
primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
409409
val (Any): The arbitrary built-in value to convert into the right form
410410
"""
411-
raise TypeError(f"Cannot deserialize bytes, type {primitive_type} not supported: {val}")
411+
raise TypeError(f"Cannot serialize to JSON, type {primitive_type} not supported: {val}")
412412

413413

414414
@to_json.register(BooleanType)
@@ -547,7 +547,7 @@ def from_json(primitive_type: PrimitiveType, val: Any) -> L: # type: ignore
547547
primitive_type (PrimitiveType): An implementation of the PrimitiveType base class.
548548
val (Any): The arbitrary JSON value to convert into the right form
549549
"""
550-
raise TypeError(f"Cannot deserialize bytes, type {primitive_type} not supported: {str(val)}")
550+
raise TypeError(f"Cannot deserialize JSON, type {primitive_type} not supported: {val}")
551551

552552

553553
@from_json.register(BooleanType)

‎tests/test_conversions.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,20 @@ def __repr__(self) -> str:
499499

500500
with pytest.raises(TypeError) as exc_info:
501501
conversions.to_bytes(FooUnknownType(), "foo") # type: ignore
502-
assert "scale does not match FooUnknownType()" in str(exc_info.value)
502+
assert "Cannot serialize to bytes, type FooUnknownType() not supported: 'foo'" in str(exc_info.value)
503503

504504
with pytest.raises(TypeError) as exc_info:
505505
conversions.from_bytes(FooUnknownType(), b"foo") # type: ignore
506506
assert "Cannot deserialize bytes, type FooUnknownType() not supported: b'foo'" in str(exc_info.value)
507507

508+
with pytest.raises(TypeError) as exc_info:
509+
conversions.to_json(FooUnknownType(), "foo") # type: ignore
510+
assert "Cannot serialize to JSON, type FooUnknownType() not supported: foo" in str(exc_info.value)
511+
512+
with pytest.raises(TypeError) as exc_info:
513+
conversions.from_json(FooUnknownType(), "foo") # type: ignore
514+
assert "Cannot deserialize JSON, type FooUnknownType() not supported: foo" in str(exc_info.value)
515+
508516

509517
@pytest.mark.parametrize(
510518
"primitive_type, value, expected_error_message",

0 commit comments

Comments
 (0)