Skip to content

fix(time): raise ValueError instead of KeyError for unsupported minimum_unit - #372

Open
MeiSiristhebest wants to merge 2 commits into
python-humanize:mainfrom
MeiSiristhebest:fix/minimum-unit-valueerror
Open

fix(time): raise ValueError instead of KeyError for unsupported minimum_unit#372
MeiSiristhebest wants to merge 2 commits into
python-humanize:mainfrom
MeiSiristhebest:fix/minimum-unit-valueerror

Conversation

@MeiSiristhebest

Copy link
Copy Markdown

Summary

When humanize.precisedelta, naturaldelta, or naturaltime are called with an unsupported unit string for minimum_unit (or suppress in precisedelta), humanize previously raised an opaque KeyError.

This PR changes the behavior to raise a clear ValueError: Minimum unit '<unit>' not supported, consistent with existing error messages when non-minimum unit enum members (e.g. years for naturaldelta) are supplied.

Behavior Change

# Before
>>> precisedelta(timedelta(seconds=1), minimum_unit="weeks")
KeyError: 'WEEKS'

# After
>>> precisedelta(timedelta(seconds=1), minimum_unit="weeks")
ValueError: Minimum unit 'weeks' not supported

Changes

  • Added _minimum_unit_or_raise helper in src/humanize/time.py.
  • Updated naturaldelta and precisedelta to catch invalid unit names and raise ValueError from None.
  • Added unit tests in tests/test_time.py for unknown minimum_unit and suppress values across precisedelta, naturaldelta, and naturaltime.

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.

1 participant