Repository navigation
Fix parsing time-only strings with a timezone-aware RELATIVE_BASE - #1362
AdrianAtZyte merged 3 commits into
Conversation
When RELATIVE_BASE is offset-aware, _correct_for_time_frame localizes dateobj to UTC so it can be compared with self.now. The subsequent tz.utcoffset(dateobj) call then receives an aware datetime, which pytz rejects with "Not naive datetime (tzinfo is already set)". The exception is not one of the two caught pytz errors, so parsing aborts and dateparser.parse() returns None for inputs like "3pm". Pass a naive copy to utcoffset() so the offset lookup works regardless of whether dateobj was localized. Behaviour with a naive RELATIVE_BASE is unchanged. Adds a regression test in tests/test_date_parser.py covering both PREFER_DATES_FROM directions with an offset-aware RELATIVE_BASE. Closes scrapinghub#1213
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1362 +/- ##
=======================================
Coverage 99.23% 99.23%
=======================================
Files 239 239
Lines 4203 4203
=======================================
Hits 4171 4171
Misses 32 32 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
serhii73
left a comment
There was a problem hiding this comment.
Checked the diff, the linked issue (#1213) and CI (15/15 green). I reproduced the bug on current master (with an offset-aware RELATIVE_BASE, parse("3pm", ...) returns None) and verified that the fix on this branch resolves to the right dates: with an aware America/Los_Angeles base, 3pm gives the previous day (base 12:00) or the same day (base 18:00) for PREFER_DATES_FROM="past", and the same day or the next day for "future"; it also works when TIMEZONE differs from the base's zone. Approving the change itself.
Non-urgent notes:
- The branch is based on 1.4.2 and conflicts with current master. The block has changed (
dateparser/parser.pyaround lines 805-821:tz_offset = tz.utcoffset(dateobj) or tz_offsetinsideif self.settings.RELATIVE_BASE:), so the naive copy has to be applied to that call during the rebase, keeping theor tz_offsetfallback becauseutcoffset()can returnNone. - The new test only asserts that parsing succeeds and that the period is
day. Asserting the resulting datetime (for example withthen_date_obj_exactly_is) and adding a base later than 3pm would catch a wrong day. - On a DST fall-back day an ambiguous time (for example
1:30amwith an aware LA base on 2020-11-01) now raisespytz.AmbiguousTimeErrorinstead of returningNone. A naiveRELATIVE_BASEalready does the same on master, so this is not new behavior for the function, but catchingpytz.AmbiguousTimeErrornext toNonExistentTimeErrorwould be a cheap hardening.
Resolve the conflicts in dateparser/parser.py and tests/test_date_parser.py. Master restructured the time-only block in _correct_for_time_frame, so the naive copy passed to utcoffset() is applied to master's version of that call, keeping its `or tz_offset` fallback. Both imports (pytest and pytz) are kept in the tests. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Master type-checks the tests with mypy in strict mode, so the new test needs parameter and return annotations. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Closes #1213
When
RELATIVE_BASEis offset-aware,_correct_for_time_framelocalizesdateobjto UTC so it can be compared withself.now, and the followingtz.utcoffset(dateobj)then gets an aware datetime, which pytz rejects withValueError: Not naive datetime. That error isn't one of the two pytz exceptions caught there, so parsing aborts andparse("3pm", ...)returnsNone. Passing a naive copy toutcoffset()fixes it; a naiveRELATIVE_BASEis unaffected.Regression test added in
tests/test_date_parser.py(bothPREFER_DATES_FROMdirections); it fails on master and passes with the fix.This change was prepared with AI assistance; the regression test was run locally and fails without the fix.