mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-119594: Improve pow(fraction.Fraction(), b, modulo) error message (#119593)
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>
This commit is contained in:
parent
bf4ff3ad2e
commit
fcca08ec2f
3 changed files with 10 additions and 1 deletions
|
@ -848,7 +848,7 @@ class Fraction(numbers.Rational):
|
||||||
|
|
||||||
__mod__, __rmod__ = _operator_fallbacks(_mod, operator.mod, False)
|
__mod__, __rmod__ = _operator_fallbacks(_mod, operator.mod, False)
|
||||||
|
|
||||||
def __pow__(a, b):
|
def __pow__(a, b, modulo=None):
|
||||||
"""a ** b
|
"""a ** b
|
||||||
|
|
||||||
If b is not an integer, the result will be a float or complex
|
If b is not an integer, the result will be a float or complex
|
||||||
|
@ -856,6 +856,8 @@ class Fraction(numbers.Rational):
|
||||||
result will be rational.
|
result will be rational.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if modulo is not None:
|
||||||
|
return NotImplemented
|
||||||
if isinstance(b, numbers.Rational):
|
if isinstance(b, numbers.Rational):
|
||||||
if b.denominator == 1:
|
if b.denominator == 1:
|
||||||
power = b.numerator
|
power = b.numerator
|
||||||
|
|
|
@ -1633,6 +1633,12 @@ class FractionTest(unittest.TestCase):
|
||||||
message % ("divmod()", "complex", "Fraction"),
|
message % ("divmod()", "complex", "Fraction"),
|
||||||
divmod, b, a)
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
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.
|
Loading…
Add table
Add a link
Reference in a new issue