Skip to content

Commit f5dda2a

Browse files
Raise EncodeError instead of ValueError for row values/types length mismatch
encode_row_values() used zip(strict=True) which raises ValueError on length mismatch. Added explicit length check to raise EncodeError, consistent with the package's exception hierarchy. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 83ae64c commit f5dda2a

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/dqlitewire/tuples.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,12 @@ def decode_row_header(data: bytes, column_count: int) -> tuple[list[ValueType] |
231231

232232
def encode_row_values(values: Sequence[Any], types: Sequence[ValueType]) -> bytes:
233233
"""Encode row values according to specified types."""
234+
if len(values) != len(types):
235+
raise EncodeError(
236+
f"Row values count ({len(values)}) does not match types count ({len(types)})"
237+
)
234238
result = bytearray()
235-
for value, vtype in zip(values, types, strict=True):
239+
for value, vtype in zip(values, types, strict=False):
236240
encoded, _ = encode_value(value, vtype)
237241
result.extend(encoded)
238242
return bytes(result)

tests/test_tuples.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,18 @@ def test_roundtrip_mixed(self) -> None:
494494
assert decoded[1] == "hello"
495495
assert abs(decoded[2] - 3.14) < 0.0001
496496

497+
def test_encode_mismatched_lengths_raises_encode_error(self) -> None:
498+
"""Mismatched values/types lengths should raise EncodeError, not ValueError."""
499+
import pytest
500+
501+
from dqlitewire.exceptions import EncodeError
502+
503+
with pytest.raises(EncodeError, match="does not match"):
504+
encode_row_values([1, 2, 3], [ValueType.INTEGER, ValueType.INTEGER])
505+
506+
with pytest.raises(EncodeError, match="does not match"):
507+
encode_row_values([1], [ValueType.INTEGER, ValueType.INTEGER])
508+
497509
def test_roundtrip_with_null(self) -> None:
498510
values = [42, None, "test"]
499511
types = [ValueType.INTEGER, ValueType.NULL, ValueType.TEXT]

0 commit comments

Comments
 (0)