@@ -94,6 +94,20 @@ def _date_and_delta(
9494 return date , _abs_timedelta (delta )
9595
9696
97+ def _minimum_unit_or_raise (name : str ) -> Unit :
98+ """Resolve *name* to a ``Unit`` or raise a clear ``ValueError``.
99+
100+ A bare ``Unit[name.upper()]`` lookup raises an opaque ``KeyError`` for an
101+ unknown unit name; this helper raises a consistent, helpful ``ValueError``
102+ instead.
103+ """
104+ try :
105+ return Unit [name .upper ()]
106+ except KeyError :
107+ msg = f"Minimum unit '{ name } ' not supported"
108+ raise ValueError (msg ) from None
109+
110+
97111def naturaldelta (
98112 value : dt .timedelta | float ,
99113 months : bool = True ,
@@ -119,6 +133,7 @@ def naturaldelta(
119133
120134 Raises:
121135 OverflowError: If `value` is too large to convert to datetime.timedelta.
136+ ValueError: If `minimum_unit` is not a supported unit.
122137
123138 Examples:
124139 Compare two timestamps in a custom local timezone::
@@ -138,11 +153,10 @@ def naturaldelta(
138153 """
139154 import datetime as dt
140155
141- tmp = Unit [ minimum_unit . upper ()]
142- if tmp not in (Unit .SECONDS , Unit .MILLISECONDS , Unit .MICROSECONDS ):
156+ min_unit = _minimum_unit_or_raise ( minimum_unit )
157+ if min_unit not in (Unit .SECONDS , Unit .MILLISECONDS , Unit .MICROSECONDS ):
143158 msg = f"Minimum unit '{ minimum_unit } ' not supported"
144159 raise ValueError (msg )
145- min_unit = tmp
146160
147161 if isinstance (value , dt .timedelta ):
148162 delta = value
@@ -275,6 +289,9 @@ def naturaltime(
275289
276290 Returns:
277291 str: A natural representation of the input in a resolution that makes sense.
292+
293+ Raises:
294+ ValueError: If `minimum_unit` is not a supported unit.
278295 """
279296 import datetime as dt
280297
@@ -534,17 +551,28 @@ def precisedelta(
534551 >>> precisedelta(delta, minimum_unit="minutes")
535552 '0 minutes'
536553
554+ ```
555+
556+ An unsupported ``minimum_unit`` raises a clear ``ValueError`` rather than
557+ an opaque ``KeyError``:
558+
559+ ```pycon
560+ >>> precisedelta(dt.timedelta(seconds=1), minimum_unit="fortnights")
561+ Traceback (most recent call last):
562+ ...
563+ ValueError: Minimum unit 'fortnights' not supported
564+
537565 ```
538566 """
539567 date , delta = _date_and_delta (value , precise = True )
540568 if date is None :
541569 return str (value )
542570
543- suppress_set = {Unit [ s . upper ()] for s in suppress }
571+ suppress_set = {_minimum_unit_or_raise ( s ) for s in suppress }
544572
545573 # Find a suitable minimum unit (it can be greater than the one that the
546574 # user gave us, if that one is suppressed).
547- min_unit = Unit [ minimum_unit . upper ()]
575+ min_unit = _minimum_unit_or_raise ( minimum_unit )
548576 min_unit = _suitable_minimum_unit (min_unit , suppress_set )
549577 del minimum_unit
550578
0 commit comments