Skip to content

Commit

Permalink
Linting and type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
kalaspuff committed Nov 24, 2019
1 parent a377960 commit 8dff5b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
42 changes: 33 additions & 9 deletions stockholm/money.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from functools import reduce
import re
from typing import Any, Iterable, Optional, Tuple, Union
from typing import Any, Iterable, Optional, Tuple, Type, Union, cast

import decimal
from decimal import Decimal, ROUND_HALF_UP
Expand Down Expand Up @@ -56,7 +56,12 @@ def sort(cls, iterable: Iterable, reverse: bool = False) -> Iterable:
return sorted(iterable, key=lambda x: x if isinstance(x, Money) else Money(x), reverse=reverse)

@classmethod
def sum(cls, iterable: Iterable, currency: Optional[Union[DefaultCurrency, Currency, str]] = DefaultCurrency, is_cents: Optional[bool] = None) -> "Money":
def sum(
cls,
iterable: Iterable,
currency: Optional[Union[Type[DefaultCurrency], Currency, str]] = DefaultCurrency,
is_cents: Optional[bool] = None,
) -> "Money":
return reduce(
lambda v, e: v + (e if isinstance(e, Money) else Money(e, is_cents=is_cents)),
iterable,
Expand All @@ -70,7 +75,7 @@ def _is_unknown_amount_type(cls, amount: Optional[Union["Money", Decimal, int, f
def __init__(
self,
amount: Optional[Union["Money", Decimal, int, float, str, object]] = None,
currency: Optional[Union[DefaultCurrency, Currency, str]] = DefaultCurrency,
currency: Optional[Union[Type[DefaultCurrency], Currency, str]] = DefaultCurrency,
is_cents: Optional[bool] = None,
units: Optional[int] = None,
nanos: Optional[int] = None,
Expand Down Expand Up @@ -102,12 +107,23 @@ def __init__(
if amount is None:
raise ConversionError("Missing input values for monetary amount")

if isinstance(amount, Money) and currency is DefaultCurrency and is_cents is None and units is None and nanos is None:
if (
isinstance(amount, Money)
and currency is DefaultCurrency
and is_cents is None
and units is None
and nanos is None
):
object.__setattr__(self, "_amount", amount._amount)
object.__setattr__(self, "_currency", amount._currency)
return

if currency is not DefaultCurrency and not isinstance(currency, str) and not isinstance(currency, Currency) and currency is not None:
if (
currency is not DefaultCurrency
and not isinstance(currency, str)
and not isinstance(currency, Currency)
and currency is not None
):
raise ConversionError("Invalid currency value")

output_amount = None
Expand Down Expand Up @@ -285,6 +301,9 @@ def amount_as_string(self, min_decimals: Optional[int] = None, max_decimals: Opt
min_decimals = min(DEFAULT_MIN_DECIMALS, max_decimals)
elif max_decimals is None:
max_decimals = max(min_decimals, DEFAULT_MAX_DECIMALS)

min_decimals = cast(int, min_decimals)
max_decimals = cast(int, max_decimals)
if min_decimals > max_decimals:
raise ValueError("Invalid values for min_decimals and max_decimals")

Expand Down Expand Up @@ -363,7 +382,7 @@ def __format__(self, format_spec: str) -> str:
output = f"{integral:{thousands_sep}.0f}"

if format_dict["align"] == "=":
format_dict["align"] = None
format_dict["align"] = ""
zeropad = True

if zeropad:
Expand All @@ -384,8 +403,8 @@ def __format__(self, format_spec: str) -> str:
output = f"{output:{fill}{align}{minimumwidth}}"

zeropad = False
sign = None
thousands_sep = None
sign = ""
thousands_sep = ""

if self._currency:
if format_dict["type"] == "m":
Expand Down Expand Up @@ -425,7 +444,12 @@ def _convert_other(self, other: Any, allow_currency_mismatch: bool = False) -> "
else:
converted_other = other

if not allow_currency_mismatch and self._currency and converted_other._currency and self._currency != converted_other._currency:
if (
not allow_currency_mismatch
and self._currency
and converted_other._currency
and self._currency != converted_other._currency
):
raise CurrencyMismatchError("Unable to perform operations on values with differing currencies")

return converted_other
Expand Down
9 changes: 7 additions & 2 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,10 @@ def test_string_formatting_sentence() -> None:
exchange_rate = Decimal("0.08861326")
m2 = Money(m1 * exchange_rate, "SEK")

expected = "I have 1,352,953 JPY which equals around 119,889.58 SEK if the exchange rate is 0.08861326 (JPY -> SEK)."
assert f"I have {m1:,.0m} which equals around {m2:,.2m} if the exchange rate is {exchange_rate} ({m1:c} -> {m2:c})." == expected
expected = (
"I have 1,352,953 JPY which equals around 119,889.58 SEK if the exchange rate is 0.08861326 (JPY -> SEK)."
)
assert (
f"I have {m1:,.0m} which equals around {m2:,.2m} if the exchange rate is {exchange_rate} ({m1:c} -> {m2:c})."
== expected
)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[flake8]
ignore = E501
ignore = E501,W503
exclude = .git,.venv,.vscode,__pycache__,build,dist

0 comments on commit 8dff5b5

Please sign in to comment.