Skip to content

Commit d977c12

Browse files
Support datetime.date in encode_value auto-inference
datetime.date objects now encode as ISO8601 text using isoformat(). Previously they raised EncodeError("Cannot infer type"), which was surprising since datetime.datetime was already supported. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 02b9eaf commit d977c12

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/dqlitewire/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ def encode_value(value: Any, value_type: ValueType | None = None) -> tuple[bytes
181181
# Naive datetime: assume UTC to match Go's always-offset format
182182
formatted += "+00:00"
183183
value = formatted
184+
elif isinstance(value, datetime.date):
185+
# Must come after datetime.datetime check (datetime is a subclass of date)
186+
value_type = ValueType.ISO8601
187+
value = value.isoformat()
184188
elif isinstance(value, str):
185189
value_type = ValueType.TEXT
186190
elif isinstance(value, bytes):

tests/test_types.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ def test_boolean_explicit_rejects_non_bool_non_int(self) -> None:
329329
with pytest.raises(EncodeError, match="[Bb]ool"):
330330
encode_value({"key": "val"}, ValueType.BOOLEAN)
331331

332+
def test_encode_date_as_iso8601(self) -> None:
333+
"""datetime.date should encode as ISO8601 text."""
334+
import datetime
335+
336+
encoded, vtype = encode_value(datetime.date(2024, 1, 15))
337+
assert vtype == ValueType.ISO8601
338+
decoded, _ = decode_value(encoded, ValueType.ISO8601)
339+
assert isinstance(decoded, datetime.datetime)
340+
assert decoded.year == 2024
341+
assert decoded.month == 1
342+
assert decoded.day == 15
343+
332344
def test_decode_integer(self) -> None:
333345
value, consumed = decode_value(encode_int64(42), ValueType.INTEGER)
334346
assert value == 42

0 commit comments

Comments
 (0)