Skip to content

Commit ee96b81

Browse files
fix: raise EncodeError when encoding None with explicit non-NULL type
encode_value(None, ValueType.INTEGER) previously silently returned NULL bytes with NULL type code, ignoring the caller's explicit type request. The early-return for None at the top of encode_value fired before the value_type parameter was inspected. Now raises EncodeError when value is None and an explicit type other than ValueType.NULL is provided. encode_value(None) and encode_value(None, ValueType.NULL) continue to work as before. Closes #094 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ab2ec61 commit ee96b81

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/dqlitewire/types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ def encode_value(value: Any, value_type: ValueType | None = None) -> tuple[bytes
172172
Returns (encoded_data, value_type).
173173
"""
174174
if value is None:
175+
if value_type is not None and value_type != ValueType.NULL:
176+
raise EncodeError(
177+
f"Cannot encode None with explicit type {value_type.name}. "
178+
f"Pass value_type=ValueType.NULL or omit value_type."
179+
)
175180
return b"\x00" * 8, ValueType.NULL
176181

177182
if value_type is None:

tests/test_types.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,28 @@ def test_encode_value_null_type_with_string_raises(self) -> None:
736736
with pytest.raises(EncodeError, match="Cannot encode non-None value"):
737737
encode_value("hello", ValueType.NULL)
738738

739+
def test_encode_value_none_with_explicit_integer_raises(self) -> None:
740+
"""None with explicit ValueType.INTEGER should raise EncodeError."""
741+
with pytest.raises(EncodeError, match="Cannot encode None with explicit type"):
742+
encode_value(None, ValueType.INTEGER)
743+
744+
def test_encode_value_none_with_explicit_text_raises(self) -> None:
745+
"""None with explicit ValueType.TEXT should raise EncodeError."""
746+
with pytest.raises(EncodeError, match="Cannot encode None with explicit type"):
747+
encode_value(None, ValueType.TEXT)
748+
749+
def test_encode_value_none_with_explicit_null_ok(self) -> None:
750+
"""None with explicit ValueType.NULL should succeed."""
751+
encoded, vtype = encode_value(None, ValueType.NULL)
752+
assert vtype == ValueType.NULL
753+
assert encoded == b"\x00" * 8
754+
755+
def test_encode_value_none_without_type_ok(self) -> None:
756+
"""None without explicit type should succeed (infer NULL)."""
757+
encoded, vtype = encode_value(None)
758+
assert vtype == ValueType.NULL
759+
assert encoded == b"\x00" * 8
760+
739761
def test_parse_iso8601_rejects_garbage(self) -> None:
740762
"""Unparseable ISO 8601 string should raise DecodeError."""
741763
from dqlitewire.types import _parse_iso8601

0 commit comments

Comments
 (0)