Strip a leading colon before parsing (: 24 February 2019) - #1355
Conversation
sanitize_date trimmed trailing colons via RE_TRIM_COLONS but left a leading colon in place, so ": 24 February 2019" parsed as None while "24 February 2019" parsed fine. Add RE_TRIM_LEADING_COLONS to remove a leading colon (and any run of them). A time such as "12:30" has digits before the colon, so it is untouched.
|
Opus 5 feedback: |
":30" was parsed as day 30 of the current month after the colon strip,
where it previously returned None. A colon followed by only digits looks
like a time fragment, not a prefixed date, so it is left alone now
(":30", ": 30" -> None again). The guard allows for whitespace inside
the lookahead, since the quantifier before it can backtrack.
|
Thanks — measured both points on the branch. Point 2 is right and is fixed in 3190fb8. Current behaviour, measured:
Point 1 I could not reproduce. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1355 +/- ##
=======================================
Coverage 97.07% 97.07%
=======================================
Files 236 236
Lines 2975 2977 +2
=======================================
+ Hits 2888 2890 +2
Misses 87 87 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Closes #889.
: 24 February 2019returnsNone, while24 February 2019parses correctly:sanitize_datealready trims trailing colons —RE_TRIM_COLONS = r"(\S.*?):*$"— so"2019:"→"2019". But a leading colon is left in place, becomes a stray token, and the parse fails.The fix adds
RE_TRIM_LEADING_COLONS = r"^\s*:+\s*"and applies it right after the existing trailing-colon trim:The important constraint is that this must not touch a colon inside a time — and it doesn't, because a time has digits before the colon (
^\s*:only matches a colon at the very start):Scope
I deliberately kept this to the leading colon, which is what the issue reports. The related label-prefix cases mentioned in passing (
at: …,Date: …) are a broader and riskier change — stripping an arbitraryword:prefix could swallow real tokens — so I left them out.on:is already handled separately byRE_SANITIZE_ON; happy to add a general label-prefix sanitizer in a follow-up if you'd like one.Tests
Extended
TestSanitizeDate.test_sanitize_date_colonswith atest_sanitize_date_leading_colonscovering the leading-colon cases and asserting"12:30"/"2019-02-24 12:30"are untouched. Reverting the source leavessanitize_date(": 24 February 2019")unchanged, so the test fails without the fix.ruff checkandruff format --check(v0.9.3, matching.pre-commit-config.yaml) are clean.tests/test_date_parser.py+tests/test_search.pyare 781 passed / 3 failed locally — the 3 are pre-existing Windows-onlydatetime.fromtimestampOSErrors intest_parse_negative_timestamp, unrelated to this change. (tests/test_date.py, which holds the new unit test, only collects on POSIX because of itsfrom time import tzsetimport, so it runs in CI rather than on my Windows box.)