pyln: fix Millisatoshi div with msat itself

Before this patch this fails: `Millisatoshi(42) / Millisatoshi(2)`
This is an operation that should return the ratio between the two
operands as a float number. Same goes for __floordiv__ operator `//`.

Changelog-None
This commit is contained in:
Michael Schmoock 2020-12-12 11:36:50 +01:00 committed by Christian Decker
parent f7cdf1dd98
commit fffc343dd7

View File

@ -176,13 +176,19 @@ class Millisatoshi:
def __sub__(self, other: 'Millisatoshi') -> 'Millisatoshi':
return Millisatoshi(int(self) - int(other))
def __mul__(self, other: int) -> 'Millisatoshi':
def __mul__(self, other: Union[int, float]) -> 'Millisatoshi':
if isinstance(other, Millisatoshi):
raise TypeError("Resulting unit msat^2 is not supported")
return Millisatoshi(floor(self.millisatoshis * other))
def __truediv__(self, other: Union[int, float]) -> 'Millisatoshi':
def __truediv__(self, other: Union[int, float, 'Millisatoshi']) -> Union['Millisatoshi', float]:
if isinstance(other, Millisatoshi):
return self.millisatoshis / other.millisatoshis
return Millisatoshi(floor(self.millisatoshis / other))
def __floordiv__(self, other: Union[int, float]) -> 'Millisatoshi':
def __floordiv__(self, other: Union[int, float, 'Millisatoshi']) -> Union['Millisatoshi', int]:
if isinstance(other, Millisatoshi):
return self.millisatoshis // other.millisatoshis
return Millisatoshi(floor(self.millisatoshis // float(other)))
def __mod__(self, other: Union[float, int]) -> 'Millisatoshi':