Skip to content

fix: dehumanize accepts singular nouns with numeric prefix ("1 day ago")#1322

Open
amitmishra11 wants to merge 1 commit into
arrow-py:masterfrom
amitmishra11:fix/dehumanize-singular-nouns
Open

fix: dehumanize accepts singular nouns with numeric prefix ("1 day ago")#1322
amitmishra11 wants to merge 1 commit into
arrow-py:masterfrom
amitmishra11:fix/dehumanize-singular-nouns

Conversation

@amitmishra11

Copy link
Copy Markdown

Pull Request Checklist

  • Added tests for changed code.
  • All tests pass when run locally.
  • Code is up-to-date with the master branch.

Description of Changes

Closes #1150

dehumanize silently rejected strings like "1 day ago" or "in 1 week" with a ValueError, while the plural form "1 days ago" worked fine.

Root cause

Singular timeframes in the locale have no {0} placeholder (e.g. "a day" for the "day" key). The search pattern built from that string is (^|\b|\d)a day, which never matches "1 day". No other pattern covers "1 day" either, so unit_visited stays all-False and the function raises.

Fix

When the primary pattern fails to match and the timeframe string has no {0} placeholder, a fallback pattern is tried using the bare unit key itself:

if not match and "{0}" not in str(time_string):
    alt_pattern = re.compile(
        rf"(^|\b)\d+\s+{re.escape(str(time_delta))}\b"
    )
    match = alt_pattern.search(input_string)

For English this catches "1 day", "1 week", "1 hour", "1 second" etc. For other locales the fallback either matches (if the unit key happens to appear in the input) or stays None — so existing behavior for non-English locales is unchanged.

Summary

Changes

  • arrow/arrow.py: add numeric fallback for singular timeframes in dehumanize
  • tests/test_arrow.py: add test_singular_with_number covering all seven en-us singular units

Testing

pytest tests/test_arrow.py -k dehumanize --no-cov  # 23 passed

Fixes #1150

'1 day ago', '1 week ago', '1 hour ago' etc. previously raised
ValueError because the primary search pattern for singular timeframes
(e.g. 'a day') never matched a numeric prefix.  A fallback pattern
using the bare unit key (e.g. r'\d+\s+day\b') is now tried when the
singular form has no {0} placeholder, making numeric and article forms
interchangeable for all en-us singular units.

Fixes arrow-py#1150
@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2224255) to head (ae01c36).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1322   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines         2315      2318    +3     
  Branches       358       359    +1     
=========================================
+ Hits          2315      2318    +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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.

The dehumanize method doesn't recognize singular nouns

1 participant