diff --git a/bindings/python/gnucash_core.py b/bindings/python/gnucash_core.py index eb21688d6c..5cefcfba53 100644 --- a/bindings/python/gnucash_core.py +++ b/bindings/python/gnucash_core.py @@ -625,8 +625,9 @@ class GncNumeric(GnuCashCoreClass): forward.__doc__ = monomorphic_operator.__doc__ def reverse(b, a): + # b is self, the right-hand operand; a is the left-hand one. if isinstance(a, (GncNumeric, int, float)): - return forward(b, a) + return forward(GncNumeric(a), b) else: return NotImplemented reverse.__name__ = '__r' + fallback_operator.__name__ + '__' diff --git a/bindings/python/tests/test_numeric.py b/bindings/python/tests/test_numeric.py index 654f497e3e..01293fe039 100644 --- a/bindings/python/tests/test_numeric.py +++ b/bindings/python/tests/test_numeric.py @@ -85,6 +85,16 @@ class TestGncNumeric(TestCase): self.assertEqual(fraction.numerator, 1000) self.assertEqual(fraction.denominator, 3) + def test_reflected_operators(self): + # A plain number on the left must stay the left-hand operand. + self.assertEqual(10 - GncNumeric(3), GncNumeric(7)) + self.assertEqual(1.5 - GncNumeric(1, 2), GncNumeric(1)) + self.assertEqual(10 / GncNumeric(4), GncNumeric(5, 2)) + self.assertEqual(1 / GncNumeric(3), GncNumeric(1, 3)) + self.assertEqual(7 // GncNumeric(2), GncNumeric(3)) + self.assertEqual(10 + GncNumeric(3), GncNumeric(13)) + self.assertEqual(10 * GncNumeric(3), GncNumeric(30)) + def test_incorect_args(self): with self.assertRaises(TypeError): GncNumeric(1, 2, 3)