https://github.com/python/cpython/commit/d02adecb42137711e8c58aac661fbeb46be4837b
commit: d02adecb42137711e8c58aac661fbeb46be4837b
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-07-16T10:44:23+03:00
summary:

[3.13] gh-119189:  Fix the power operator for Fraction (GH-119242) (GH-119836)

When using the ** operator or pow() with Fraction as the base
and an exponent that is not rational, a float, or a complex, the
fraction is no longer converted to a float.
(cherry picked from commit b9965ef282d6662145d2e05b080c811132ce6fde)

Co-authored-by: Joshua Herman <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-05-20-13-48-37.gh-issue-119189.dhJVs5.rst
M Lib/fractions.py
M Lib/test/test_fractions.py
M Misc/ACKS

diff --git a/Lib/fractions.py b/Lib/fractions.py
index f8c6c9c438c737..e71ef58f18b3db 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -875,8 +875,10 @@ def __pow__(a, b):
                 # A fractional power will generally produce an
                 # irrational number.
                 return float(a) ** float(b)
-        else:
+        elif isinstance(b, (float, complex)):
             return float(a) ** b
+        else:
+            return NotImplemented
 
     def __rpow__(b, a):
         """a ** b"""
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 3a714c64278847..7e20c5e8e2bce9 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -922,21 +922,21 @@ def testMixedPower(self):
         self.assertTypedEquals(Root(4) ** F(2, 1), Root(4, F(1)))
         self.assertTypedEquals(Root(4) ** F(-2, 1), Root(4, -F(1)))
         self.assertTypedEquals(Root(4) ** F(-2, 3), Root(4, -3.0))
-        self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('1.5 ** 
X'))
+        self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('3/2 ** 
X'))
         self.assertEqual(SymbolicReal('X') ** F(3, 2), SymbolicReal('X ** 
1.5'))
 
-        self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(2.25, 0.0))
-        self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(1.0, 0.0))
+        self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(F(9,4), 0.0))
+        self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(F(1), 0.0))
         self.assertTypedEquals(F(3, 2) ** RectComplex(2, 0), Polar(2.25, 0.0))
         self.assertTypedEquals(F(1, 1) ** RectComplex(2, 3), Polar(1.0, 0.0))
         self.assertTypedEquals(Polar(4, 2) ** F(3, 2), Polar(8.0, 3.0))
         self.assertTypedEquals(Polar(4, 2) ** F(3, 1), Polar(64, 6))
         self.assertTypedEquals(Polar(4, 2) ** F(-3, 1), Polar(0.015625, -6))
         self.assertTypedEquals(Polar(4, 2) ** F(-3, 2), Polar(0.125, -3.0))
-        self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('1.5 
** X'))
+        self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('3/2 
** X'))
         self.assertEqual(SymbolicComplex('X') ** F(3, 2), SymbolicComplex('X 
** 1.5'))
 
-        self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('1.5 ** X'))
+        self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('3/2 ** X'))
         self.assertEqual(Symbolic('X') ** F(3, 2), Symbolic('X ** 1.5'))
 
     def testMixingWithDecimal(self):
diff --git a/Misc/ACKS b/Misc/ACKS
index d5ee1f84f95088..80504a16bce64f 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -751,6 +751,7 @@ Kasun Herath
 Chris Herborth
 Ivan Herman
 Jürgen Hermann
+Joshua Jay Herman
 Gary Herron
 Ernie Hershey
 Thomas Herve
diff --git 
a/Misc/NEWS.d/next/Library/2024-05-20-13-48-37.gh-issue-119189.dhJVs5.rst 
b/Misc/NEWS.d/next/Library/2024-05-20-13-48-37.gh-issue-119189.dhJVs5.rst
new file mode 100644
index 00000000000000..e5cfbcf95a0b81
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-05-20-13-48-37.gh-issue-119189.dhJVs5.rst
@@ -0,0 +1,3 @@
+When using the ``**`` operator or :func:`pow` with :class:`~fractions.Fraction`
+as the base and an exponent that is not rational, a float, or a complex, the
+fraction is no longer converted to a float.

_______________________________________________
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