Skip to content

Parse "midi" as noon in French (#459) - #1536

Merged
serhii73 merged 3 commits into
masterfrom
fix/459-french-prochain-midi
Oct 19, 2026
Merged

serhii73 merged 3 commits into
masterfrom
fix/459-french-prochain-midi

Conversation

@serhii73

@serhii73 serhii73 commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #459.

>>> settings = {"RELATIVE_BASE": datetime(2026, 10, 7, 9, 30)}  # a Wednesday
>>> parse("mardi à midi", languages=["fr"], settings=settings)
None                                   # master
datetime.datetime(2026, 10, 6, 12, 0)  # this branch

mardi midi, demain à midi and a bare midi behave the same way. This fixes the midi part of the issue. lundi prochain, and so the title example lundi prochain à midi, still does not parse: see Not included.

Root cause

French has no entry for midi, so the word is unknown and any string with it is not a date. In search_dates the time was silently lost: On se voit demain à midi. returned demain à without the time (2000-01-02 00:00 with RELATIVE_BASE=datetime(2000, 1, 1)). English gets noon from a simplification in en.yaml, (?:12\s+)?noon: '12:00'.

What changed

One simplification at the end of the list in fr.yaml, and fr.py regenerated with python -m dateparser_scripts.write_complete_data. Before the edit the generator reproduces all 205 committed language files byte for byte, so fr.py is the only generated file that changes.

- (?<!(?:apr[eè]s|avant)(?:[\W_]+le)?[\W_]+)midi: '12:00'

The lookbehind is the exclusion the issue asks for: après-midi is the afternoon and avant midi is before noon, not noon. A hyphen counts as a non-word character for the automatic word boundary, so a plain midi matches inside après-midi: après-midi would become noon today and demain après-midi a date with a UTC−12 offset (the 12:00 read as a time zone). With the exclusion they return None exactly as on master. The details:

  • [\W_]+ and not [\s-]: parse() normalises spaces and dashes before the simplifications run, but search_dates simplifies the raw text, where a [\s-] lookbehind lets midi through after two spaces, an en dash or a non-breaking hyphen. _ is in there because the boundary every rule is wrapped in counts _ as a separator.
  • apr[eè]s because search_dates strips the accents of the text before simplifying it, whatever NORMALIZE says, while with NORMALIZE off the rules keep theirs. With après the exclusion never matched there: Réunion demain après-midi lost its demain.
  • The optional le covers après le midi, which otherwise became noon today.
  • The 12 prefix of the English pattern is not copied: 12 midi is not French, and the prefix would drop the day of le 12 midi.
  • The lookbehind is variable-width but linear: about 1 ms for 16,000 spaces before midi, about 4 ms for 64,000.
  • The rule is at the end of the list, where its position does not matter, because Parse "at 22h" and "à 22h" as a time of day, not as hours ago #1441 inserts its own rules in the middle and the two would conflict.

Not included

Known limitations

Making midi a word has the side effects noon has in English:

  • search_dates reads a standalone Midi as noon, proper nouns included: Le Canal du Midi est beau. returns Midi at 12:00, Le Midi Libre du 12 octobre 2026 titrait en une. gains Le Midi, and Rendez-vous à la gare de Bruxelles-Midi demain loses its demain.
  • With languages=None, a short date phrase in another language that contains the loanword MIDI can be detected as French: Ich habe am 5. Mai 2020 ein MIDI-Keyboard gekauft. is German on master and French here. With IGNORE_SURROUNDING_TEXT, parse("Midi Libre 12 octobre 2026") goes from 2026-10-12 to None.
  • The exclusion looks at the words before midi in the same sentence: search_dates(strategy="ngram") reads n-grams on their own, so Rendez-vous cet après midi returns midi at 12:00 (the default strategy returns None), and so does a line break between après and midi.
  • midi et demi and midi 30 are not understood as 12:30: parse("midi et demi") is None as before, search_dates reads Il est midi et demi. as 12:00, and parse("midi 30") reads the 30 as a day (2026-10-30 12:00).

Tests

Nothing existing was touched, and no existing test ID changes (new test methods only): 5 files, +223 −0.

  • test_languages.py: test_french_midi_translation, 3 rows (midi, Mardi à midi, 12 octobre 2026 à midi).
  • test_date_parser.py:
    • test_french_midi_is_noon: 20 rows with a fixed RELATIVE_BASE: the two forms of the issue (mardi midi, mardi à midi), the other six weekdays, midi, à midi, demain à midi, hier midi, après-demain à midi, avant-hier midi, two dates (le 12 midi, 2026-10-12 midi), upper case, PREFER_DATES_FROM: future and NORMALIZE: False.
    • test_french_midi_is_noon_when_french_is_detected: 2 rows without languages.
    • test_french_midi_after_apres_or_avant_is_not_noon: 14 negatives (après-midi, après midi, l'après-midi, après le midi, avant midi, avant-midi, demain après-midi, lundi après-midi, mardi avant midi, upper case, three with _, NORMALIZE: False), all still None.
    • test_french_dates_that_were_parsed_before_midi_are_unchanged: 8 regression rows (lundi, mardi, mardi à 12:00, mardi 14:30, demain, après-demain, il y a 3 jours, 18 octobre 2012 à 19 h 21 min).
    • test_french_next_weekday: lundi prochain, mardi prochain and lundi prochain à midi as strict xfails (raises=AssertionError).
  • test_search.py: test_search_dates_with_french_midi, 11 rows: two with midi, and nine checking that après-midi (hyphen, en dash, non-breaking hyphen, _, NORMALIZE: False, with le) and avant midi are not read as noon. parse() normalises dashes and spaces first, so the rows that tell [\W_]+ from [\s-] have to go through search_dates.

On master source 27 of the new tests fail, exactly the rows that expect noon; the 31 others (negatives and regression rows) pass there as controls. I also mutated the pattern twelve ways (no exclusion, only après, only avant, [\s-], [\s-]+, no _, an accent-sensitive après, no optional le, an over-broad .* lookbehind, the English 12 prefix, two other replacements): each mutant is caught by at least one new test.

Verification

  • Full suite, same environment: 24820 passed, 17 skipped, 4 xfailed with this change, against 24762 passed, 17 skipped, 1 xfailed on master. That is the 58 new passing tests plus the 3 xfails. tests/test_dateparser_data_integrity.py, which regenerates every language in memory and compares it with the committed files, passes.

  • A differential run of master against this branch (about 750,000 parse/search_dates/translate calls per tree, 29 settings) found no change for strings without midi and none for the other 459 locales.

  • pre-commit run --all-files is clean.

  • I have read the contributing guide and set up pre-commit.

🤖 Generated with Claude Code

French had no entry for "midi", so a date string with it was not
understood: "mardi midi", "mardi à midi" and "demain à midi" returned
None, and search_dates dropped the time of "demain à midi".

Add a simplification that reads "midi" as 12:00, like English does for
"noon". "après-midi" (the afternoon) and "avant midi" (before noon) are
not noon, so they are excluded, also with an article in between ("après
le midi") and with any run of non-word characters between the words. A
hyphen counts as a word boundary, so without the exclusion a plain
"midi" would match inside "après-midi", and "demain après-midi" would be
read as a time zone offset of -12:00.

"<weekday> prochain" is not covered: it needs support for "next
<weekday>", which master lacks even in English (#573). The examples of
the issue that need it are strict xfails.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.96%. Comparing base (fed9cf9) to head (45bed24).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1536   +/-   ##
=======================================
  Coverage   98.96%   98.96%           
=======================================
  Files         239      239           
  Lines        3592     3592           
=======================================
  Hits         3555     3555           
  Misses         37       37           

☔ 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 Oct 5, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks


Comparing fix/459-french-prochain-midi (45bed24) with master (fed9cf9)

Open in CodSpeed

serhii73 and others added 2 commits October 5, 2026 13:37
- The comment above the new tests gave a wrong reason for using a
  Wednesday: the seven weekdays resolve to seven different dates
  whatever the current date.
- The parse() rows with a non-breaking hyphen and a no-break space
  could not tell \W+ from [\s-], because sanitize_date normalises both
  before the simplifications run. Only search_dates sees the raw text,
  so the rows that pin \W+ (an en dash and a non-breaking hyphen) are
  search_dates rows now.
- Nothing pinned that "après" and "avant" only exclude the "midi" that
  directly follows, or that "12 midi" keeps its day: add
  "après-demain à midi", "avant-hier midi" and "le 12 midi".
- Rows inserted in the middle of test_translation and
  test_search_and_parse renumbered 390 existing test IDs. The new rows
  are in new test methods instead, so no existing test ID changes.
- The strict xfails only tolerate an AssertionError, and say that the
  French entries are needed besides the support for "next <weekday>".

Also move the rule to the end of the simplifications of fr.yaml. Their
order does not matter for it, and this keeps it away from the place
where the open #1441 inserts its own rules.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Two cases slipped past the exclusion of "après" and "avant", both found
by comparing master and the branch on large corpora:

- search_dates always strips the accents of the text before it
  simplifies it, whatever NORMALIZE says, but with NORMALIZE off the
  rules keep their accents, so the "après" of the lookbehind never
  matched. "Réunion demain après-midi" lost its "demain" and "Livraison
  après midi" became noon. Write the rule as apr[eè]s, which the
  normalised rule set turns into apr[ee]s.
- Every rule is wrapped in a boundary that counts "_" as a separator, so
  "après_midi" is a standalone "midi", but \W+ did not cross the "_" and
  it became noon. Use [\W_]+, like the boundary.

Add the rows that fail without these changes, and rows for what the
exclusion must not do: "après-demain à midi" and "avant-hier midi" (the
existing relative expressions that contain the words it keys on), and
"2026-10-12 midi", next to "le 12 midi" for the missing "12 " prefix.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@AdrianAtZyte

Copy link
Copy Markdown
Member

Until then lundi prochain à midi still gives None in parse()

Should we then not mark this PR as fixing the issue, just as being part of the solution?

@AdrianAtZyte AdrianAtZyte left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, also cut the tests down: drop the prochain xfails and the “unchanged” regression block, and keep a handful of positive and negative rows, similar to #1519. The description could be a lot shorter too.

(feel free to ignore it and merge as is, "too many tests" is not an issue I think we need to prioritize at the moment 🙂 )

@serhii73

Copy link
Copy Markdown
Collaborator Author

This is approved and CI is green. The branch is 5 commits behind master, but I merged current master locally: no conflicts, and the full test suite passes (24844 passed). Could a maintainer merge it when convenient?

On Adrian's question above: this PR only fixes the midi part of #459, because lundi prochain à midi still needs <weekday> prochain (after #1401). If GitHub closes #459 on merge because of the Fixes #459 line, please reopen it. I'll open the follow-up PR once #1401 and this one are merged.

@serhii73
serhii73 merged commit 337d7f5 into master Oct 19, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

French lundi prochain à midi not parsed

2 participants