https://github.com/python/cpython/commit/fcca08ec2f48f4ba5ba1d4690fb39b1efe630944
commit: fcca08ec2f48f4ba5ba1d4690fb39b1efe630944
branch: main
author: Wim Jeantine-Glenn <[email protected]>
committer: terryjreedy <[email protected]>
date: 2024-05-29T13:46:20-04:00
summary:

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 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-05-26-22-22-51.gh-issue-119594.fnQNM8.rst
M Lib/fractions.py
M Lib/test/test_fractions.py

diff --git a/Lib/fractions.py b/Lib/fractions.py
index f8c6c9c438c737..f91b4f35eff370 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -848,7 +848,7 @@ def _mod(a, b):
 
     __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 @@ def __pow__(a, b):
         result will be rational.
 
         """
+        if modulo is not None:
+            return NotImplemented
         if isinstance(b, numbers.Rational):
             if b.denominator == 1:
                 power = b.numerator
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 3a714c64278847..3648a8982a37e0 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -1633,6 +1633,12 @@ def test_complex_handling(self):
                                  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
index 00000000000000..d2de5273edf571
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-05-26-22-22-51.gh-issue-119594.fnQNM8.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to