diff options
author | Jonah Lawrence <jonah@freshidea.com> | 2023-01-11 01:54:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-11 08:54:11 +0000 |
commit | 53637ddbacaef2474429b22176091a362ce6567f (patch) | |
tree | 45318cc88faad0c81935f0ae4a99c3437e7aca4e /babel/units.py | |
parent | 61097845764f5a6afc2251172168e1b1732b290f (diff) | |
download | babel-53637ddbacaef2474429b22176091a362ce6567f.tar.gz |
Add type annotations (#934)
Refs e.g. https://github.com/python/typeshed/pull/9455
Co-authored-by: Spencer Brown <spencerb21@live.com>
Co-authored-by: Aarni Koskela <akx@iki.fi>
Diffstat (limited to 'babel/units.py')
-rw-r--r-- | babel/units.py | 45 |
1 files changed, 35 insertions, 10 deletions
diff --git a/babel/units.py b/babel/units.py index f8f2675..1180bd1 100644 --- a/babel/units.py +++ b/babel/units.py @@ -1,13 +1,24 @@ +from __future__ import annotations + +import decimal +from typing import TYPE_CHECKING + from babel.core import Locale -from babel.numbers import format_decimal, LC_NUMERIC +from babel.numbers import LC_NUMERIC, format_decimal +if TYPE_CHECKING: + from typing_extensions import Literal class UnknownUnitError(ValueError): - def __init__(self, unit, locale): + def __init__(self, unit: str, locale: Locale) -> None: ValueError.__init__(self, f"{unit} is not a known unit in {locale}") -def get_unit_name(measurement_unit, length='long', locale=LC_NUMERIC): +def get_unit_name( + measurement_unit: str, + length: Literal['short', 'long', 'narrow'] = 'long', + locale: Locale | str | None = LC_NUMERIC, +) -> str | None: """ Get the display name for a measurement unit in the given locale. @@ -36,7 +47,7 @@ def get_unit_name(measurement_unit, length='long', locale=LC_NUMERIC): return locale.unit_display_names.get(unit, {}).get(length) -def _find_unit_pattern(unit_id, locale=LC_NUMERIC): +def _find_unit_pattern(unit_id: str, locale: Locale | str | None = LC_NUMERIC) -> str | None: """ Expand an unit into a qualified form. @@ -62,7 +73,13 @@ def _find_unit_pattern(unit_id, locale=LC_NUMERIC): return unit_pattern -def format_unit(value, measurement_unit, length='long', format=None, locale=LC_NUMERIC): +def format_unit( + value: float | decimal.Decimal, + measurement_unit: str, + length: Literal['short', 'long', 'narrow'] = 'long', + format: str | None = None, + locale: Locale | str | None = LC_NUMERIC, +) -> str: """Format a value of a given unit. Values are formatted according to the locale's usual pluralization rules @@ -132,7 +149,11 @@ def format_unit(value, measurement_unit, length='long', format=None, locale=LC_N return f"{formatted_value} {fallback_name or measurement_unit}" # pragma: no cover -def _find_compound_unit(numerator_unit, denominator_unit, locale=LC_NUMERIC): +def _find_compound_unit( + numerator_unit: str, + denominator_unit: str, + locale: Locale | str | None = LC_NUMERIC, +) -> str | None: """ Find a predefined compound unit pattern. @@ -181,10 +202,14 @@ def _find_compound_unit(numerator_unit, denominator_unit, locale=LC_NUMERIC): def format_compound_unit( - numerator_value, numerator_unit=None, - denominator_value=1, denominator_unit=None, - length='long', format=None, locale=LC_NUMERIC -): + numerator_value: float | decimal.Decimal, + numerator_unit: str | None = None, + denominator_value: float | decimal.Decimal = 1, + denominator_unit: str | None = None, + length: Literal["short", "long", "narrow"] = "long", + format: str | None = None, + locale: Locale | str | None = LC_NUMERIC, +) -> str | None: """ Format a compound number value, i.e. "kilometers per hour" or similar. |