Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect BaseCurrency subclass defaults #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stockholm/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ def __new__(
) -> BaseCurrency:
if not cls._meta:
raise TypeError("'BaseCurrency' object is not callable")

if currency and isinstance(currency, str):
currency = getattr(cls, currency, currency)

return cast(
BaseCurrency,
BaseCurrencyType(
Expand Down
35 changes: 35 additions & 0 deletions tests/test_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,41 @@ class EUR(BaseCurrency):
assert str(m) == "100 CarlosCoin"


def test_custom_currency_attributes():
class _XYZ(BaseCurrency):
ticker = "XYZ"
decimal_digits = 5
interchangeable_with = ("CNH", "RMB")
preferred_ticker = "ZYX"

class Currency(BaseCurrency):
XYZ = _XYZ

c1 = Currency("XYZ")
assert c1 != Money(0, Currency.XYZ)
assert c1.decimal_digits == _XYZ.decimal_digits
assert c1.interchangeable_with == _XYZ.interchangeable_with
assert c1.preferred_ticker == _XYZ.preferred_ticker

c2 = Currency(c1)
assert c2.ticker == "XYZ"
assert c2 == c1
assert str(c2) == "XYZ"
assert c2 == "XYZ"

c3 = Currency()
assert c3.ticker == ""
assert c3 != c1
assert c3 == ""
assert str(c3) == ""
assert c3 != Money(0, Currency.XYZ)

c4 = Currency(Currency.XYZ)
assert c4.ticker == "XYZ"
assert c4 == c1
assert c4 == "XYZ"


def test_currency_hashable() -> None:
class CNY(BaseCurrency):
interchangeable_with = ("CNH", "RMB")
Expand Down