Repository navigation
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 intoNov 13, 2026
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
AdrianAtZyte
approved these changes
Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
parse()withdate_formatsthat contain%B,%b,%Aor%araisedKeyErrorfor a month or weekday name that is spelled with a letter that theregexmodule equates with a letter of the name when it ignores case, but thatstr.lower()does not map back to it. For exampleparse("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 bymatch.lower(). The lookup now triesmatch.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
KeyErrorfor 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 fixedRELATIVE_BASE:parse()withdate_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 andlower()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 raisedKeyErrorno longer do (2,007 of them return a date, 227 returnNone, 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).KeyErrorwas 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 loggedKeyError.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 runningregexmodule 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_dateswithstrategy="ngram"anddate_formatsfor 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