perps(klear): reject non-canonical dates in margin-report range validation (#409)#417
Conversation
…ation Closes #409. `_validate_date_range` used `datetime.date.fromisoformat`, which (3.11+) leniently accepts non-canonical forms like "20260501" or ISO week dates ("2026-W22-1") and then forwarded the raw string to a server expecting strict YYYY-MM-DD. Now, after parsing, the validator asserts `parsed.isoformat() == original` and rejects anything non-canonical at the SDK boundary. Test: extends the existing date-range guard test with the basic-format and week-date cases (both parse but are non-canonical). mypy strict + ruff + 46 klear margin tests green.
Code Review — PR #417perps(klear): reject non-canonical dates in margin-report range validation (#409) OverviewThis is a focused, surgical fix for a real correctness bug: Code QualityStrengths:
Nit — comment length: The 3-line comment is justified here (explains a genuinely non-obvious Python 3.11 behavior), but per CLAUDE.md convention it could be tightened to one line: # 3.11+ fromisoformat accepts "20260605" / ISO week dates — reject non-canonical forms before forwarding raw to server.This is minor; the current comment is fine. Just noting it for consistency with the project's one-line-max guideline. CorrectnessThe fix is correct. One clarifying note for the record:
No issues. Test CoverageGood: Both non-canonical forms are tested, one each as One minor gap: Neither test exercises the symmetric cases (basic format as with pytest.raises(ValueError, match="canonical YYYY-MM-DD"):
auth_klear_client.margin.margin_reports(start_date="2026-05-01", end_date="20260601")Not a blocker. Security / Performance
CLAUDE.md ConventionsAll checked:
VerdictApprove. Clean, correct, minimal fix for a real bug. The comment nit and optional extra test case are both minor. Good work. |
Cluster C of the post-review follow-ups — a focused validation fix.
#409
_validate_date_range(Klearmargin_reports) parsed withdatetime.date.fromisoformat, which on Python 3.11+ leniently accepts non-canonical inputs —"20260501"(basic format) and ISO week dates ("2026-W22-1") — and then forwarded the raw string to a server expecting strictYYYY-MM-DD, so it passed SDK validation but could be rejected (or misinterpreted) downstream.Fix: after parsing, assert
parsed.isoformat() == originaland raise a clear error for any non-canonical form.Verification
"20260501"→2026-05-01and"2026-W22-1"→2026-05-25both parse (so they reach the new canonical check) but are non-canonical.mypy --strict+ruffclean; 46 Klear margin tests pass.🤖 Generated with Claude Code