Skip to content

Commit

Permalink
Fixing a bug by remove the implicit cast to float if the class implem…
Browse files Browse the repository at this point in the history
…ents __rpow__ .

The fraction module would cast classes that have __rpow__ into floats. This change
removes the implicit cast and if __rpow__ isn't there it will raise an exception
  • Loading branch information
zitterbewegung committed May 20, 2024
1 parent 16b46eb commit 8d9d1d5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,13 @@ 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"""
if b._denominator == 1 and b._numerator >= 0:
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ James Henstridge
Kasun Herath
Chris Herborth
Ivan Herman
Joshua Jay Herman
Jürgen Hermann
Gary Herron
Ernie Hershey
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes a bug where fractions will not be casted into float when given as
argument to __rpow__

0 comments on commit 8d9d1d5

Please sign in to comment.