]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-119594: Improve pow(fraction.Fraction(), b, modulo) error message (#119593)
authorWim Jeantine-Glenn <hey@wimglenn.com>
Wed, 29 May 2024 17:46:20 +0000 (12:46 -0500)
committerGitHub <noreply@github.com>
Wed, 29 May 2024 17:46:20 +0000 (13:46 -0400)
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>
Lib/fractions.py
Lib/test/test_fractions.py
Misc/NEWS.d/next/Library/2024-05-26-22-22-51.gh-issue-119594.fnQNM8.rst [new file with mode: 0644]

index f8c6c9c438c73799fe1a9c1c2fd7ebb19dd59718..f91b4f35eff37083c64f8427bceefff9672c2883 100644 (file)
@@ -848,7 +848,7 @@ class Fraction(numbers.Rational):
 
     __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
@@ -856,6 +856,8 @@ class Fraction(numbers.Rational):
         result will be rational.
 
         """
+        if modulo is not None:
+            return NotImplemented
         if isinstance(b, numbers.Rational):
             if b.denominator == 1:
                 power = b.numerator
index 3a714c642788476fa70fbde7690ba79125609e2f..3648a8982a37e05215dfe629073ae8b976d03f5a 100644 (file)
@@ -1633,6 +1633,12 @@ class FractionTest(unittest.TestCase):
                                  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()
diff --git a/Misc/NEWS.d/next/Library/2024-05-26-22-22-51.gh-issue-119594.fnQNM8.rst b/Misc/NEWS.d/next/Library/2024-05-26-22-22-51.gh-issue-119594.fnQNM8.rst
new file mode 100644 (file)
index 0000000..d2de527
--- /dev/null
@@ -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.