1
0

Compare commits

..

No commits in common. "42ec581aca9eadea0f86fda612f240dd90068a05" and "b720fd04e7b85e5259b60b3dd466e36b7a93fdbd" have entirely different histories.

2 changed files with 22 additions and 23 deletions

View File

@ -506,12 +506,6 @@ class AssetInformation:
THREE_DECIMALS_UNDER_THIS_AMOUNT = 0.10
def assert_same_asset(do: str, a_asset: 'AssetAmount', b_asset: 'AssetAmount'):
if a_asset.asset != b_asset.asset:
msg = f'Attempting to {do} {a_asset} and {b_asset}, but these must have the same asset'
raise ValueError(msg)
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True, eq=True, slots=True)
class AssetAmount:
@ -553,7 +547,11 @@ class AssetAmount:
return other
if other.is_zero():
return self
assert_same_asset('add', self, other)
if self.asset != other.asset:
msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
)
raise ValueError(msg)
return AssetAmount(self.asset, self.amount + other.amount)
@ -566,12 +564,18 @@ class AssetAmount:
return -other
if other.is_zero():
return self
assert_same_asset('subtract', self, other)
if self.asset != other.asset:
msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
)
raise ValueError(msg)
return AssetAmount(self.asset, self.amount - other.amount)
def __truediv__(self, other: 'Decimal | AssetAmount') -> 'Decimal | AssetAmount':
if isinstance(other, AssetAmount):
assert_same_asset('divide', self, other)
if self.asset != other.asset:
msg = f'Attempting to divide {self} by {other}, but these must have the same asset'
raise ValueError(msg)
return self.amount / other.amount
return AssetAmount(self.asset, self.amount / other)
@ -584,7 +588,11 @@ class AssetAmount:
raise TypeError(msg)
if self.is_zero() or other.is_zero():
return self.amount - other.amount
assert_same_asset('compare', self, other)
if self.asset != other.asset:
msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
)
raise ValueError(msg)
return self.amount - other.amount
def is_zero(self) -> bool:

View File

@ -1,4 +1,3 @@
import re
from decimal import Decimal
import pytest
@ -84,9 +83,7 @@ def test_add_wrong_type():
def test_add_wrong_asset():
with pytest.raises(
ValueError,
match=re.escape(
'Attempting to add $20.00 USD and 21.00 DKK, but these must have the same asset',
),
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
assert USD_20 + DKK_21
@ -99,9 +96,7 @@ def test_sub_wrong_type():
def test_sub_wrong_asset():
with pytest.raises(
ValueError,
match=re.escape(
'Attempting to subtract $20.00 USD and 21.00 DKK, but these must have the same asset',
),
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
assert USD_20 - DKK_21
@ -114,9 +109,7 @@ def test_cmp_wrong_type():
def test_cmp_wrong_asset():
with pytest.raises(
ValueError,
match=re.escape(
'Attempting to compare $20.00 USD and 21.00 DKK, but these must have the same asset',
),
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
assert USD_20 < DKK_21
@ -124,8 +117,6 @@ def test_cmp_wrong_asset():
def test_div_wrong_asset():
with pytest.raises(
ValueError,
match=re.escape(
'Attempting to divide $21.00 USD and 21.00 DKK, but these must have the same asset',
),
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
assert USD_21 / DKK_21