improve typing

This commit is contained in:
Pablo Martin 2024-06-27 18:11:22 +02:00
parent 0bd0d7c862
commit 2ab9ca5456

View file

@ -12,11 +12,13 @@ DEFAULT_MONEY_PRECISION = Decimal(
class MoneyAmount: class MoneyAmount:
def __init__(self, amount, currency): def __init__(
self, amount: Union[int, Decimal, str], currency: Union[str, Currency]
) -> "MoneyAmount":
self.amount = self._parse_amount(amount) self.amount = self._parse_amount(amount)
self.currency = self._parse_currency(currency) self.currency = self._parse_currency(currency)
def _parse_amount(self, amount: Union[int, Decimal, str]): def _parse_amount(self, amount: Union[int, Decimal, str]) -> Decimal:
if isinstance(amount, (int, Decimal)): if isinstance(amount, (int, Decimal)):
return Decimal(amount).quantize(DEFAULT_MONEY_PRECISION) return Decimal(amount).quantize(DEFAULT_MONEY_PRECISION)
elif isinstance(amount, str): elif isinstance(amount, str):
@ -27,7 +29,7 @@ class MoneyAmount:
else: else:
raise TypeError(f"Amount must be int, Decimal, or str, not {type(amount)}") raise TypeError(f"Amount must be int, Decimal, or str, not {type(amount)}")
def _parse_currency(self, currency): def _parse_currency(self, currency: Union[str, Currency]) -> Currency:
if isinstance(currency, Currency): if isinstance(currency, Currency):
return currency return currency
if isinstance(currency, str): if isinstance(currency, str):