Skip to content

Add parse_many() to infer a shared date order - #1438

Open
AdrianAtZyte wants to merge 12 commits into
scrapinghub:masterfrom
AdrianAtZyte:batch-date-order
Open

AdrianAtZyte wants to merge 12 commits into
scrapinghub:masterfrom
AdrianAtZyte:batch-date-order

Conversation

@AdrianAtZyte

@AdrianAtZyte AdrianAtZyte commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Resolves #1099, resolves #1167, fixes #539, fixes #804, fixes #413.

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.23%. Comparing base (ce8c439) to head (616dbac).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing AdrianAtZyte:batch-date-order (616dbac) with master (ce8c439)

Open in CodSpeed

@AdrianAtZyte
AdrianAtZyte marked this pull request as ready for review September 23, 2026 15:33

@serhii73 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, YMD

On 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.

@AdrianAtZyte

Copy link
Copy Markdown
Member Author

It works on this branch's base, but it breaks once merged with current master, because of #1431

Fixed in the merge with master, with the STRICT_DATE_ORDER check you suggested in the retry condition.

#1443 introduces a STRICT_DATE_ORDER setting as well, with narrower semantics

Since the setting here enforces the position of every number, it already covers the #1443 case, e.g. parse("32 DEC 10", settings={"DATE_ORDER": "DMY", "STRICT_DATE_ORDER": True}) is None here. So I wonder if #1443 could build on this one instead of adding its own meaning for the setting. I am not sure there is a use case for enforcing the year position but not the day/month one; if there is one, I would rather give that a separate name.

@serhii73 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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 and list(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 and parse_many gives 02:49. The function promises to match parse(), so the base should come from the same timezone-aware helper.
  2. 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-item None or an on_error option would suit CSV-style input better; unparseable strings already become None.
  3. Ambiguity handling. list(parse_many(['01/02/2015'])) and ['11.12.13'] raise AmbiguousDateOrderError where parse() returns a date, and an all-ambiguous column raises unless DATE_ORDER is 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.
  4. Interaction with STRICT_DATE_ORDER. A user-supplied STRICT_DATE_ORDER (e.g. 'none') is silently replaced by 'all', and DATE_ORDER is dropped once eliminated. Given that STRICT_DATE_ORDER is now a public setting, this should be documented or reconciled.
  5. Validation and memory. date_orders=[] silently means "all orders" and a bad value only fails deep inside the first strict parse. With DATE_ORDER set, 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 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving yet:

  • STRICT_DATE_ORDER already exists on master with the values none, year and all (dateparser/conf.py, from #1443). parse_many() silently replaces it with all. 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 local now() is used).
  • One check is still pending.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants