Repository navigation
Add parse_many() to infer a shared date order - #1438
AdrianAtZyte wants to merge 12 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1438 +/- ##
=======================================
Coverage 99.22% 99.23%
=======================================
Files 239 239
Lines 4256 4306 +50
=======================================
+ Hits 4223 4273 +50
Misses 33 33 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
serhii73
left a comment
There was a problem hiding this comment.
Thanks, parse_many() is a neat way to settle the day/month question for a whole column. It works on this branch's base,
but it breaks once merged with current master, because of #1431 (merged today). #1431 makes _parser.parse retry with day and
month swapped on any ValueError (other than "Fields missing") when DATE_ORDER is set explicitly, and the new
strict-order ValueError falls into that retry:
import dateparser
dateparser.parse('15-12-18', settings={'DATE_ORDER': 'MDY', 'STRICT_DATE_ORDER': True})
# this branch: None master + this PR: datetime(2018, 12, 15) (the docs example)
list(dateparser.parse_many(['2015/1/1', '2015/2/3', '2015/31/12']))
# this branch: [2015-01-01, 2015-03-02, 2015-12-31]
# master + this PR: AmbiguousDateOrderError: Ambiguous date order, could be any of: YDM, YMDOn the merge, 8 of the new tests fail (TestStrictDateOrder 2 and 4, test_parse_many_0/1/2, test_lazy,
test_max_pending, test_no_fitting_order_0). Skipping the retry when the setting is on fixes it. With this in the retry's
raise condition, the whole of test_clean_api.py, test_date_parser.py and test_parser.py passes on the merge:
if (
"DATE_ORDER" not in settings._mod_settings
or settings.STRICT_DATE_ORDER
or str(error).startswith("Fields missing")
or cls._has_month_name(tokens)
):
raise(A dedicated ValueError subclass for the strict check that the retry lets through would work too.)
Also, heads-up: #1443 introduces a STRICT_DATE_ORDER setting as well, with narrower semantics (it only enforces where a
two-digit year may appear, and still reads a swapped day/month). We should agree on one name and meaning before either lands.
|
serhii73
left a comment
There was a problem hiding this comment.
Code review (claude code-review, at 1d9a62a; probes run on this head, and parse_many does not exist on master). This adds a new public function, so I'd like a few behaviours settled before approving:
- Relative dates ignore
TIMEZONE.settings.setdefault("RELATIVE_BASE", datetime.now())uses naive local time. With{"TIMEZONE": "Pacific/Kiritimati", "RETURN_AS_TIMEZONE_AWARE": True},parse("now")gives 2026-10-20 23:49+14:00 andlist(parse_many(["now"]))gives 2026-10-20 11:49; with{"TIMEZONE": "UTC", "TO_TIMEZONE": "Pacific/Kiritimati"},parse("in 1 hour")gives 00:49 on the 21st andparse_manygives 02:49. The function promises to matchparse(), so the base should come from the same timezone-aware helper. - One bad row ends the iteration.
['01/02/2015', '25/12/2015', '12/25/2015', '01/01/2016']yields two dates, then raises'12/25/2015' does not fit the DMY date order, and the generator is finished, so every later row is lost. A per-itemNoneor anon_erroroption would suit CSV-style input better; unparseable strings already becomeNone. - Ambiguity handling.
list(parse_many(['01/02/2015']))and['11.12.13']raiseAmbiguousDateOrderErrorwhereparse()returns a date, and an all-ambiguous column raises unlessDATE_ORDERis passed. With yearless input the error lists DMY, DYM, YDM and YMD, orders nobody wants. Please confirm that raising by default is the intended contract, and whether the locale default should be the fallback. - Interaction with
STRICT_DATE_ORDER. A user-suppliedSTRICT_DATE_ORDER(e.g.'none') is silently replaced by'all', andDATE_ORDERis dropped once eliminated. Given thatSTRICT_DATE_ORDERis now a public setting, this should be documented or reconciled. - Validation and memory.
date_orders=[]silently means "all orders" and a bad value only fails deep inside the first strict parse. WithDATE_ORDERset, nothing streams until the end (max_pending=None), so a large file is held entirely in memory even though the caller's order could be used immediately.
Smaller points: every junk or empty row is parsed twice after the order is locked and up to seven times while it is not; AmbiguousDateOrderError cannot be resumed, so yielded rows are parsed again; the docs example has no import or code marker and does not list max_pending or date_orders; use from collections import deque instead of the extra module import.
serhii73
left a comment
There was a problem hiding this comment.
Not approving yet:
STRICT_DATE_ORDERalready exists on master with the valuesnone,yearandall(dateparser/conf.py, from #1443).parse_many()silently replaces it withall. Please use the existing setting as it is.- Earlier points are still open: a bad row ends the generator, ambiguous single inputs raise, and relative dates ignore
TIMEZONE(a naive localnow()is used). - One check is still pending.
Resolves #1099, resolves #1167, fixes #539, fixes #804, fixes #413.