Skip to content

Do not raise KeyError for a month or weekday name spelled with a case-fold variant in date_formats - #1579

Merged
AdrianAtZyte merged 1 commit into
scrapinghub:masterfrom
serhii73:fix-date-formats-casefold
Nov 13, 2026
Merged

AdrianAtZyte merged 1 commit into
scrapinghub:masterfrom
serhii73:fix-date-formats-casefold

Conversation

@serhii73

Copy link
Copy Markdown
Collaborator

parse() with date_formats that contain %B, %b, %A or %a raised KeyError for a month or weekday name that is spelled with a letter that the regex module equates with a letter of the name when it ignores case, but that str.lower() does not map back to it. For example parse("5 ARALIK 2020", date_formats=["%d %B %Y"], languages=["tr"]) (the name is "aralık"), parse("ſeptember 5 2020", date_formats=["%B %d %Y"]) ("ſ" for "s"), parse("5 µάρ 2020", date_formats=["%d %B %Y"], languages=["el"]) (the micro sign for "μ") and the same with the Greek final sigma, "İ" for "i" and others. DateDataParser.get_date_data() raised it as well. search_dates(..., strategy="ngram", date_formats=[...]) did not raise, because it catches and logs the error for each candidate, but it found only a part of the date or a wrong date for such a spelling (for "5 ſept 2020" and "%d %b %Y" it returned [("2020", ...)]); it now finds the date.

The names are found with a pattern compiled with re.IGNORECASE, and then looked up by match.lower(). The lookup now tries match.lower() first, exactly as before, and only when that is not a known name it looks for the known names that the same pattern equates with the match (re.fullmatch(re.escape(name), match, flags=re.IGNORECASE)) and reads the match as those. A match that is none of the known names gives no translation instead of an exception (defensive: the pattern only matches the known names, and with the data of the shipped locales every spelling that it matches is read as a name). The names of a text are resolved once per distinct spelling.

Results of calls that did not hit the error do not change. The invariant is: a call gives a different result if and only if the lookup raised KeyError for it on master, whether the error reached the caller (parse(), get_date_data()) or was caught and logged (search_dates()). Measured on master and on this branch in separate processes with a fixed RELATIVE_BASE:

  • parse() with date_formats: 7,721 calls (eight languages and automatic detection, month and weekday names in plain, upper, title and swapped case, with every letter of every name replaced by every variant that the matcher accepts and lower() does not map back, several names in one text, surrounding words, formats without a name directive, texts that are not dates). The 2,234 calls that raised KeyError no longer do (2,007 of them return a date, 227 return None, as the normally spelled text would); the other 5,487 give the same result.
  • search_dates(..., strategy="ngram", date_formats=[...]): 951 calls (every eighth case of the same corpus). KeyError was logged for 236 of them on master and for none on the branch; 234 results changed (228 a different date or span, 6 from nothing to a date), none of them a call without a logged KeyError.

The generators and comparison scripts are not part of the change.

Tests: the examples above for each of %B, %b, %A, %a (skipped when the running regex module does not equate the two letters), a sweep of all code points that finds the English letters with such a variant at run time (skipped when there is none), a deterministic test with a pattern and a mapping that disagree, search_dates with strategy="ngram" and date_formats for the same spellings (no warning logged, the whole date found), and golden cases for names of known spelling that pass before and after.

🤖 Generated with Claude Code

The pattern that finds month and weekday names in a date string for
date_formats with %B, %b, %A or %a ignores case the way the regex module
does, which also equates letters that str.lower() keeps apart, such as "ſ"
and "s", the Greek final sigma and sigma, the micro sign and mu, or the
Turkish "ı" and "I". The names were then looked up by str.lower() of the
match, so such a spelling (for example "5 ARALIK 2020" with languages=["tr"]
and "%d %B %Y") raised KeyError out of parse() instead of being translated.

When the lowercase match is not a known name, look for the names that the
pattern equates with it, and read the match as those. A match that is none
of the known names gives no translation instead of an exception (defensive:
the pattern only matches known names). Names that were found by their
lowercase form are looked up as before, so only a call for which the lookup
raised KeyError changes its result. That includes
search_dates(..., strategy="ngram", date_formats=...), where the error was
caught and logged for each candidate and such a spelling gave a missing or a
partial date; it now gives the date.
@codspeed

codspeed Bot commented Nov 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing serhii73:fix-date-formats-casefold (d676450) with master (90544c0)

Open in CodSpeed

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.24%. Comparing base (90544c0) to head (d676450).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1579   +/-   ##
=======================================
  Coverage   99.23%   99.24%           
=======================================
  Files         239      239           
  Lines        4203     4213   +10     
=======================================
+ Hits         4171     4181   +10     
  Misses         32       32           

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

@AdrianAtZyte
AdrianAtZyte merged commit 67eb603 into scrapinghub:master Nov 13, 2026
20 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.

2 participants