Skip to content

Commit 65fc7a5

Browse files
Add type check for explicit ValueType.BOOLEAN in encode_value
Previously, encode_value with explicit BOOLEAN used Python truthiness to coerce any value (strings, lists, dicts) to True/False silently. Now rejects non-bool/int types with a clear EncodeError. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 918f48d commit 65fc7a5

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/dqlitewire/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ def encode_value(value: Any, value_type: ValueType | None = None) -> tuple[bytes
189189
raise EncodeError(f"Cannot infer type for value: {type(value)}")
190190

191191
if value_type == ValueType.BOOLEAN:
192+
if not isinstance(value, (bool, int)):
193+
raise EncodeError(f"Expected bool or int for BOOLEAN, got {type(value).__name__}")
192194
return encode_uint64(1 if value else 0), value_type
193195
elif value_type in (ValueType.INTEGER, ValueType.UNIXTIME):
194196
if isinstance(value, bool):

tests/test_types.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ def test_boolean_uses_uint64_encoding(self) -> None:
318318
assert decode_uint64(encoded_true) == 1
319319
assert decode_uint64(encoded_false) == 0
320320

321+
def test_boolean_explicit_rejects_non_bool_non_int(self) -> None:
322+
"""encode_value with explicit BOOLEAN should reject non-bool/int types."""
323+
import pytest
324+
325+
with pytest.raises(EncodeError, match="[Bb]ool"):
326+
encode_value("hello", ValueType.BOOLEAN)
327+
with pytest.raises(EncodeError, match="[Bb]ool"):
328+
encode_value([1, 2], ValueType.BOOLEAN)
329+
with pytest.raises(EncodeError, match="[Bb]ool"):
330+
encode_value({"key": "val"}, ValueType.BOOLEAN)
331+
321332
def test_decode_integer(self) -> None:
322333
value, consumed = decode_value(encode_int64(42), ValueType.INTEGER)
323334
assert value == 42

0 commit comments

Comments
 (0)