If one calls pow(fractions.Fraction, x, module) with modulo not None, the error message now says that the types are incompatible rather than saying pow only takes 2 arguments. Implemented by having fractions.Fraction __pow__ accept optional modulo argument and return NotImplemented if not None. pow() then raises with appropriate message.
---------
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
__mod__, __rmod__ = _operator_fallbacks(_mod, operator.mod, False)
- def __pow__(a, b):
+ def __pow__(a, b, modulo=None):
"""a ** b
If b is not an integer, the result will be a float or complex
result will be rational.
"""
+ if modulo is not None:
+ return NotImplemented
if isinstance(b, numbers.Rational):
if b.denominator == 1:
power = b.numerator
message % ("divmod()", "complex", "Fraction"),
divmod, b, a)
+ def test_three_argument_pow(self):
+ message = "unsupported operand type(s) for ** or pow(): '%s', '%s', '%s'"
+ self.assertRaisesMessage(TypeError,
+ message % ("Fraction", "int", "int"),
+ pow, F(3), 4, 5)
+
if __name__ == '__main__':
unittest.main()
--- /dev/null
+If one calls pow(fractions.Fraction, x, module) with modulo not None, the error message now says that the types are incompatible rather than saying pow only takes 2 arguments. Patch by Wim Jeantine-Glenn and Mark Dickinson.