fix: replace fragile datetime string slicing with robust parser#20
fix: replace fragile datetime string slicing with robust parser#20
Conversation
Replace hardcoded `res[:23]` truncation with `_parse_datetime()` helper that preserves microsecond precision and handles variable-length fractional seconds, timezone suffixes, and trailing whitespace. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2d81900931
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| frac_digits = frac_end - dot - 1 | ||
| if frac_digits > 6: | ||
| s = s[: dot + 7] + s[frac_end:] | ||
| return datetime.fromisoformat(s) |
There was a problem hiding this comment.
Normalize trailing Z suffix before parsing datetime
pyproject.toml supports Python 3.10, but datetime.fromisoformat() on 3.10 rejects RFC3339 UTC strings ending in Z. _parse_datetime() now keeps timezone suffixes after truncating fractional digits, so inputs like 2022-06-03T12:24:35.123456789Z raise ValueError in supported runtimes when a driver returns UTC timestamps in Z form; the previous res[:23] path dropped that suffix and parsed successfully. This turns previously accepted query results into runtime failures for query(..., datetime).
Useful? React with 👍 / 👎.
- Normalize Z suffix to +00:00 for Python 3.10 compatibility - Add error logging at call site before ValueError propagates - Fix incorrect skipif guard on timezone test (+HH:MM works on 3.7+) - Add tests: Z suffix, 7-digit boundary, leading whitespace, nanoseconds with timezone, empty string Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Remove skipif on test_nanosecond_precision_with_timezone — after truncation the string is valid on Python 3.10 - Add test_nanosecond_precision_with_z_suffix (combined Z + truncation) - Add test_trailing_dot_raises (malformed fractional seconds) - Remove unused sys import Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Expand docstring to document all normalizations (whitespace, Z suffix, truncation) - Add defensive guards for empty/missing datetime results, matching the existing int path - Enrich error log with database name and SQL context - Add tests: all-nines truncation boundary, negative timezone offset Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
res[:23]truncation inbase.pywith a_parse_datetime()helper that truncates sub-microsecond precision to 6 digits before callingfromisoformat()Closes #6
Test plan
uv run python -m pytest tests/test_datetime_parsing.py -v— 7/7 new tests passuv run python -m pytest tests/test_query.py -v— 16/16 existing tests passres[:23]inbase.py🤖 Generated with Claude Code