1+ from __future__ import annotations
2+
3+ import decimal
4+
5+ from typing_extensions import Literal
6+
17from babel .core import Locale
2- from babel .numbers import format_decimal , LC_NUMERIC
8+ from babel .localedata import LocaleDataDict
9+ from babel .numbers import LC_NUMERIC , format_decimal
310
411
512class UnknownUnitError (ValueError ):
6- def __init__ (self , unit , locale ):
13+ def __init__ (self , unit : str , locale : Locale ):
714 ValueError .__init__ (self , f"{ unit } is not a known unit in { locale } " )
815
916
10- def get_unit_name (measurement_unit , length = 'long' , locale = LC_NUMERIC ):
17+ def get_unit_name (measurement_unit : str , length : Literal ["short" , "long" , "narrow" ] = 'long' ,
18+ locale : Locale | str | None = LC_NUMERIC ) -> str | None :
1119 """
1220 Get the display name for a measurement unit in the given locale.
1321
@@ -36,7 +44,7 @@ def get_unit_name(measurement_unit, length='long', locale=LC_NUMERIC):
3644 return locale .unit_display_names .get (unit , {}).get (length )
3745
3846
39- def _find_unit_pattern (unit_id , locale = LC_NUMERIC ):
47+ def _find_unit_pattern (unit_id : str , locale : Locale | str | None = LC_NUMERIC ) -> str | None :
4048 """
4149 Expand an unit into a qualified form.
4250
@@ -62,7 +70,9 @@ def _find_unit_pattern(unit_id, locale=LC_NUMERIC):
6270 return unit_pattern
6371
6472
65- def format_unit (value , measurement_unit , length = 'long' , format = None , locale = LC_NUMERIC ):
73+ def format_unit (value : float | decimal .Decimal , measurement_unit : str ,
74+ length : Literal ["short" , "long" , "narrow" ] = 'long' , format : str | None = None ,
75+ locale : Locale | str | None = LC_NUMERIC ) -> str :
6676 """Format a value of a given unit.
6777
6878 Values are formatted according to the locale's usual pluralization rules
@@ -132,7 +142,7 @@ def format_unit(value, measurement_unit, length='long', format=None, locale=LC_N
132142 return f"{ formatted_value } { fallback_name or measurement_unit } " # pragma: no cover
133143
134144
135- def _find_compound_unit (numerator_unit , denominator_unit , locale = LC_NUMERIC ):
145+ def _find_compound_unit (numerator_unit : str , denominator_unit : str , locale : Locale | str | None = LC_NUMERIC ) -> str | None :
136146 """
137147 Find a predefined compound unit pattern.
138148
@@ -181,10 +191,10 @@ def _find_compound_unit(numerator_unit, denominator_unit, locale=LC_NUMERIC):
181191
182192
183193def format_compound_unit (
184- numerator_value , numerator_unit = None ,
185- denominator_value = 1 , denominator_unit = None ,
186- length = 'long' , format = None , locale = LC_NUMERIC
187- ) :
194+ numerator_value : float | decimal . Decimal , numerator_unit : str | None = None ,
195+ denominator_value : float | decimal . Decimal = 1 , denominator_unit : str | None = None ,
196+ length : Literal [ "short" , "long" , "narrow" ] = 'long' , format : str | None = None ,
197+ locale : Locale | str | None = LC_NUMERIC ) -> str | None :
188198 """
189199 Format a compound number value, i.e. "kilometers per hour" or similar.
190200
0 commit comments