Skip to content

Commit ec484b6

Browse files
[3.13] gh-119189: Add yet more tests for mixed Fraction arithmetic (GH-119298) (GH-119346)
(cherry picked from commit 10b1bd9) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent f757996 commit ec484b6

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

Lib/test/test_fractions.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ def __str__(self):
135135
def __repr__(self):
136136
return f'{self.__class__.__name__}({self.value!r})'
137137

138+
class SymbolicReal(Symbolic):
139+
pass
140+
numbers.Real.register(SymbolicReal)
141+
142+
class SymbolicComplex(Symbolic):
143+
pass
144+
numbers.Complex.register(SymbolicComplex)
145+
138146
class Rat:
139147
"""Simple Rational class for testing mixed arithmetic."""
140148
def __init__(self, n, d):
@@ -273,6 +281,8 @@ def __repr__(self):
273281
return f'{self.__class__.__name__}({self.x!r}, {self.y!r})'
274282
numbers.Complex.register(Rect)
275283

284+
class RectComplex(Rect, complex):
285+
pass
276286

277287
class FractionTest(unittest.TestCase):
278288

@@ -790,12 +800,17 @@ def testMixedMultiplication(self):
790800

791801
self.assertTypedEquals(F(3, 2) * Root(4), Root(F(9, 1)))
792802
self.assertTypedEquals(Root(4) * F(3, 2), 3.0)
803+
self.assertEqual(F(3, 2) * SymbolicReal('X'), SymbolicReal('3/2 * X'))
804+
self.assertRaises(TypeError, operator.mul, SymbolicReal('X'), F(3, 2))
793805

794806
self.assertTypedEquals(F(3, 2) * Polar(4, 2), Polar(F(6, 1), 2))
795807
self.assertTypedEquals(F(3, 2) * Polar(4.0, 2), Polar(6.0, 2))
796808
self.assertTypedEquals(F(3, 2) * Rect(4, 3), Rect(F(6, 1), F(9, 2)))
809+
self.assertTypedEquals(F(3, 2) * RectComplex(4, 3), RectComplex(6.0+0j, 4.5+0j))
797810
self.assertRaises(TypeError, operator.mul, Polar(4, 2), F(3, 2))
798811
self.assertTypedEquals(Rect(4, 3) * F(3, 2), 6.0 + 4.5j)
812+
self.assertEqual(F(3, 2) * SymbolicComplex('X'), SymbolicComplex('3/2 * X'))
813+
self.assertRaises(TypeError, operator.mul, SymbolicComplex('X'), F(3, 2))
799814

800815
self.assertEqual(F(3, 2) * Symbolic('X'), Symbolic('3/2 * X'))
801816
self.assertRaises(TypeError, operator.mul, Symbolic('X'), F(3, 2))
@@ -815,12 +830,16 @@ def testMixedDivision(self):
815830

816831
self.assertTypedEquals(F(2, 3) / Root(4), Root(F(1, 9)))
817832
self.assertTypedEquals(Root(4) / F(2, 3), 3.0)
833+
self.assertEqual(F(3, 2) / SymbolicReal('X'), SymbolicReal('3/2 / X'))
834+
self.assertRaises(TypeError, operator.truediv, SymbolicReal('X'), F(3, 2))
818835

819836
self.assertTypedEquals(F(3, 2) / Polar(4, 2), Polar(F(3, 8), -2))
820837
self.assertTypedEquals(F(3, 2) / Polar(4.0, 2), Polar(0.375, -2))
821838
self.assertTypedEquals(F(3, 2) / Rect(4, 3), Rect(0.24, 0.18))
822839
self.assertRaises(TypeError, operator.truediv, Polar(4, 2), F(2, 3))
823840
self.assertTypedEquals(Rect(4, 3) / F(2, 3), 6.0 + 4.5j)
841+
self.assertEqual(F(3, 2) / SymbolicComplex('X'), SymbolicComplex('3/2 / X'))
842+
self.assertRaises(TypeError, operator.truediv, SymbolicComplex('X'), F(3, 2))
824843

825844
self.assertEqual(F(3, 2) / Symbolic('X'), Symbolic('3/2 / X'))
826845
self.assertRaises(TypeError, operator.truediv, Symbolic('X'), F(2, 3))
@@ -857,9 +876,14 @@ def testMixedIntegerDivision(self):
857876

858877
self.assertRaises(TypeError, operator.mod, F(2, 3), Root(4))
859878
self.assertTypedEquals(Root(4) % F(3, 2), 0.5)
879+
self.assertEqual(F(3, 2) % SymbolicReal('X'), SymbolicReal('3/2 % X'))
880+
self.assertRaises(TypeError, operator.mod, SymbolicReal('X'), F(3, 2))
860881

861882
self.assertRaises(TypeError, operator.mod, F(3, 2), Polar(4, 2))
883+
self.assertRaises(TypeError, operator.mod, F(3, 2), RectComplex(4, 3))
862884
self.assertRaises(TypeError, operator.mod, Rect(4, 3), F(2, 3))
885+
self.assertEqual(F(3, 2) % SymbolicComplex('X'), SymbolicComplex('3/2 % X'))
886+
self.assertRaises(TypeError, operator.mod, SymbolicComplex('X'), F(3, 2))
863887

864888
self.assertEqual(F(3, 2) % Symbolic('X'), Symbolic('3/2 % X'))
865889
self.assertRaises(TypeError, operator.mod, Symbolic('X'), F(2, 3))
@@ -888,7 +912,6 @@ def testMixedPower(self):
888912
self.assertIsInstance(F(4, 9) ** Rat(-3, 2), float)
889913
self.assertAlmostEqual(F(4, 9) ** Rat(-3, 2), 3.375)
890914
self.assertAlmostEqual(F(-4, 9) ** Rat(-3, 2), 3.375j)
891-
892915
self.assertTypedEquals(Rat(9, 4) ** F(3, 2), 3.375)
893916
self.assertTypedEquals(Rat(3, 2) ** F(3, 1), Rat(27, 8))
894917
self.assertTypedEquals(Rat(3, 2) ** F(-3, 1), F(8, 27))
@@ -899,16 +922,22 @@ def testMixedPower(self):
899922
self.assertTypedEquals(Root(4) ** F(2, 1), Root(4, F(1)))
900923
self.assertTypedEquals(Root(4) ** F(-2, 1), Root(4, -F(1)))
901924
self.assertTypedEquals(Root(4) ** F(-2, 3), Root(4, -3.0))
925+
self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('1.5 ** X'))
926+
self.assertEqual(SymbolicReal('X') ** F(3, 2), SymbolicReal('X ** 1.5'))
902927

903928
self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(2.25, 0.0))
904929
self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(1.0, 0.0))
930+
self.assertTypedEquals(F(3, 2) ** RectComplex(2, 0), Polar(2.25, 0.0))
931+
self.assertTypedEquals(F(1, 1) ** RectComplex(2, 3), Polar(1.0, 0.0))
905932
self.assertTypedEquals(Polar(4, 2) ** F(3, 2), Polar(8.0, 3.0))
906933
self.assertTypedEquals(Polar(4, 2) ** F(3, 1), Polar(64, 6))
907934
self.assertTypedEquals(Polar(4, 2) ** F(-3, 1), Polar(0.015625, -6))
908935
self.assertTypedEquals(Polar(4, 2) ** F(-3, 2), Polar(0.125, -3.0))
936+
self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('1.5 ** X'))
937+
self.assertEqual(SymbolicComplex('X') ** F(3, 2), SymbolicComplex('X ** 1.5'))
909938

910-
self.assertTypedEquals(F(3, 2) ** Symbolic('X'), Symbolic('1.5 ** X'))
911-
self.assertTypedEquals(Symbolic('X') ** F(3, 2), Symbolic('X ** 1.5'))
939+
self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('1.5 ** X'))
940+
self.assertEqual(Symbolic('X') ** F(3, 2), Symbolic('X ** 1.5'))
912941

913942
def testMixingWithDecimal(self):
914943
# Decimal refuses mixed arithmetic (but not mixed comparisons)

0 commit comments

Comments
 (0)