From fffc343dd77ed80787dda85355db29693b7bc1b0 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Sat, 12 Dec 2020 11:36:50 +0100 Subject: [PATCH] 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 --- contrib/pyln-client/pyln/client/lightning.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/contrib/pyln-client/pyln/client/lightning.py b/contrib/pyln-client/pyln/client/lightning.py index 20abe9567..a4e7b7df3 100644 --- a/contrib/pyln-client/pyln/client/lightning.py +++ b/contrib/pyln-client/pyln/client/lightning.py @@ -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':