@@ -72,10 +72,11 @@ def _find_unit_pattern(unit_id: str, locale: Locale | str | None = LC_NUMERIC) -
7272 for unit_pattern in sorted (unit_patterns , key = len ):
7373 if unit_pattern .endswith (unit_id ):
7474 return unit_pattern
75+ return None
7576
7677
7778def format_unit (
78- value : float | decimal .Decimal ,
79+ value : str | float | decimal .Decimal ,
7980 measurement_unit : str ,
8081 length : Literal ['short' , 'long' , 'narrow' ] = 'long' ,
8182 format : str | None = None ,
@@ -184,28 +185,28 @@ def _find_compound_unit(
184185 # units like "kilometer" or "hour" into actual units like "length-kilometer" and
185186 # "duration-hour".
186187
187- numerator_unit = _find_unit_pattern (numerator_unit , locale = locale )
188- denominator_unit = _find_unit_pattern (denominator_unit , locale = locale )
188+ resolved_numerator_unit = _find_unit_pattern (numerator_unit , locale = locale )
189+ resolved_denominator_unit = _find_unit_pattern (denominator_unit , locale = locale )
189190
190191 # If either was not found, we can't possibly build a suitable compound unit either.
191- if not (numerator_unit and denominator_unit ):
192+ if not (resolved_numerator_unit and resolved_denominator_unit ):
192193 return None
193194
194195 # Since compound units are named "speed-kilometer-per-hour", we'll have to slice off
195196 # the quantities (i.e. "length", "duration") from both qualified units.
196197
197- bare_numerator_unit = numerator_unit .split ("-" , 1 )[- 1 ]
198- bare_denominator_unit = denominator_unit .split ("-" , 1 )[- 1 ]
198+ bare_numerator_unit = resolved_numerator_unit .split ("-" , 1 )[- 1 ]
199+ bare_denominator_unit = resolved_denominator_unit .split ("-" , 1 )[- 1 ]
199200
200201 # Now we can try and rebuild a compound unit specifier, then qualify it:
201202
202203 return _find_unit_pattern (f"{ bare_numerator_unit } -per-{ bare_denominator_unit } " , locale = locale )
203204
204205
205206def format_compound_unit (
206- numerator_value : float | decimal .Decimal ,
207+ numerator_value : str | float | decimal .Decimal ,
207208 numerator_unit : str | None = None ,
208- denominator_value : float | decimal .Decimal = 1 ,
209+ denominator_value : str | float | decimal .Decimal = 1 ,
209210 denominator_unit : str | None = None ,
210211 length : Literal ["short" , "long" , "narrow" ] = "long" ,
211212 format : str | None = None ,
@@ -289,7 +290,11 @@ def format_compound_unit(
289290 denominator_value = ""
290291
291292 formatted_denominator = format_unit (
292- denominator_value , denominator_unit , length = length , format = format , locale = locale
293+ denominator_value ,
294+ measurement_unit = (denominator_unit or "" ),
295+ length = length ,
296+ format = format ,
297+ locale = locale ,
293298 ).strip ()
294299 else : # Bare denominator
295300 formatted_denominator = format_decimal (denominator_value , format = format , locale = locale )
0 commit comments