Skip to content

Do not raise ValueError for digits that int() does not know - #1581

Merged
AdrianAtZyte merged 3 commits into
scrapinghub:masterfrom
serhii73:fix-digits-int-rejects
Nov 13, 2026
Merged

AdrianAtZyte merged 3 commits into
scrapinghub:masterfrom
serhii73:fix-digits-int-rejects

Conversation

@serhii73

Copy link
Copy Markdown
Collaborator

The regex module can read as digits characters that were added to Unicode after the Unicode version of the running Python, and int() rejects them. Three places convert such a digit with int() without checking it, so parse() and search_dates() raised ValueError for a text that holds one:

  • a timestamp, e.g. ten of these digits with DEFAULT_LANGUAGES set or with IGNORE_SURROUNDING_TEXT enabled (date.py);
  • the hour before a part of the day, e.g. 5 UTC-1200 <digit> night (_parts_of_day.py);
  • the number after 20 or 30 in a Russian number pair, e.g. 20 <digit> (locale.py).

One commit per place. Each treats the text as not matching, like any other text that is not a timestamp, an hour or a compound ordinal. The existing checks are kept: only a text that raised ValueError is affected.

Results of texts that worked before do not change: 3,600 calls of parse() and search_dates() on timestamps, parts of the day and Russian number pairs written with the digits of eight scripts (ASCII, Arabic-Indic, Persian, Devanagari, Bengali, Thai, fullwidth, Mongolian) and with digits that int() rejects give the same result on master and on this branch, except that the 378 calls that raised ValueError no longer do.

Each commit has a test that makes int() fail by patching it, so it runs on every Python and regex version, and tests with real digits that the regex module knows and int() rejects (digits_unknown_to_int() in tests/__init__.py, found at run time; skipped when there are none). The later commits reuse the helper of the first.

🤖 Generated with Claude Code

The timestamp parser finds its digits with a regular expression of the regex
module, whose \d knows digits that were added to Unicode after the Unicode
version of this Python, and converts them with int(), which rejects them.
Such digits made parse() and search_dates() raise ValueError, e.g. when
DEFAULT_LANGUAGES is set or IGNORE_SURROUNDING_TEXT is enabled, instead of
the text not being a timestamp.
The part-of-day step finds the hour before words such as "night" with a
regular expression of the regex module, whose \d knows digits that were added
to Unicode after the Unicode version of this Python, and converts it with
int(), which rejects them. Such a digit before a part of the day made parse()
and search_dates() raise ValueError, e.g. in "5 UTC-1200 <digit> night".
Treat it like a number that is not an hour.
The Russian compound ordinals ("двадцать первое") are combined with int(),
after the numbers of two neighbouring tokens were found with \d of the regex
module, which knows digits that were added to Unicode after the Unicode
version of this Python and which int() rejects. "20 <digit>" made parse() and
search_dates() raise ValueError, with default settings. Leave such a pair as
it is, as for any other number that is not combined.
@codspeed

codspeed Bot commented Nov 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing serhii73:fix-digits-int-rejects (d8bc3fb) 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 (d8bc3fb).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1581   +/-   ##
=======================================
  Coverage   99.23%   99.24%           
=======================================
  Files         239      239           
  Lines        4203     4212    +9     
=======================================
+ Hits         4171     4180    +9     
  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 8c1a272 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