Skip to content

Commit 02e57e5

Browse files
committed
Update test_numeric_tower.py from Cpython v3.11.2
1 parent 6c6290d commit 02e57e5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Lib/test/test_numeric_tower.py

+30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@
1414
_PyHASH_MODULUS = sys.hash_info.modulus
1515
_PyHASH_INF = sys.hash_info.inf
1616

17+
18+
class DummyIntegral(int):
19+
"""Dummy Integral class to test conversion of the Rational to float."""
20+
21+
def __mul__(self, other):
22+
return DummyIntegral(super().__mul__(other))
23+
__rmul__ = __mul__
24+
25+
def __truediv__(self, other):
26+
return NotImplemented
27+
__rtruediv__ = __truediv__
28+
29+
@property
30+
def numerator(self):
31+
return DummyIntegral(self)
32+
33+
@property
34+
def denominator(self):
35+
return DummyIntegral(1)
36+
37+
1738
class HashTest(unittest.TestCase):
1839
def check_equal_hash(self, x, y):
1940
# check both that x and y are equal and that their hashes are equal
@@ -113,6 +134,8 @@ def test_decimals(self):
113134
self.check_equal_hash(D('12300.00'), D(12300))
114135
self.check_equal_hash(D('12300.000'), D(12300))
115136

137+
# TODO: RUSTPYTHON
138+
@unittest.expectedFailure
116139
def test_fractions(self):
117140
# check special case for fractions where either the numerator
118141
# or the denominator is a multiple of _PyHASH_MODULUS
@@ -121,6 +144,13 @@ def test_fractions(self):
121144
self.assertEqual(hash(F(7*_PyHASH_MODULUS, 1)), 0)
122145
self.assertEqual(hash(F(-_PyHASH_MODULUS, 1)), 0)
123146

147+
# The numbers ABC doesn't enforce that the "true" division
148+
# of integers produces a float. This tests that the
149+
# Rational.__float__() method has required type conversions.
150+
x = F(DummyIntegral(1), DummyIntegral(2), _normalize=False)
151+
self.assertRaises(TypeError, lambda: x.numerator/x.denominator)
152+
self.assertEqual(float(x), 0.5)
153+
124154
def test_hash_normalization(self):
125155
# Test for a bug encountered while changing long_hash.
126156
#

0 commit comments

Comments
 (0)