MCPcopy Index your code
hub / github.com/python/cpython / test_floatconversion

Method test_floatconversion

Lib/test/test_float.py:207–252  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

205 self.assertAlmostEqual(float(" .25e-1 "), .025)
206
207 def test_floatconversion(self):
208 # Make sure that calls to __float__() work properly
209 class Foo2(float):
210 def __float__(self):
211 return 42.
212
213 class Foo3(float):
214 def __new__(cls, value=0.):
215 return float.__new__(cls, 2*value)
216
217 def __float__(self):
218 return self
219
220 class Foo4(float):
221 def __float__(self):
222 return 42
223
224 # Issue 5759: __float__ not called on str subclasses (though it is on
225 # unicode subclasses).
226 class FooStr(str):
227 def __float__(self):
228 return float(str(self)) + 1
229
230 self.assertEqual(float(FloatLike(42.)), 42.)
231 self.assertEqual(float(Foo2()), 42.)
232 with self.assertWarns(DeprecationWarning):
233 self.assertEqual(float(Foo3(21)), 42.)
234 self.assertRaises(TypeError, float, Foo4(42))
235 self.assertEqual(float(FooStr('8')), 9.)
236
237 self.assertRaises(TypeError, time.sleep, FloatLike(""))
238
239 # Issue #24731
240 f = FloatLike(OtherFloatSubclass(42.))
241 with self.assertWarns(DeprecationWarning):
242 self.assertEqual(float(f), 42.)
243 with self.assertWarns(DeprecationWarning):
244 self.assertIs(type(float(f)), float)
245 with self.assertWarns(DeprecationWarning):
246 self.assertEqual(FloatSubclass(f), 42.)
247 with self.assertWarns(DeprecationWarning):
248 self.assertIs(type(FloatSubclass(f)), FloatSubclass)
249
250 self.assertEqual(float(MyIndex(42)), 42.0)
251 self.assertRaises(OverflowError, float, MyIndex(2**2000))
252 self.assertRaises(TypeError, float, MyInt(42))
253
254 def test_keyword_args(self):
255 with self.assertRaisesRegex(TypeError, 'keyword argument'):

Callers

nothing calls this directly

Calls 13

Foo3Class · 0.85
Foo4Class · 0.85
OtherFloatSubclassClass · 0.85
assertWarnsMethod · 0.80
FloatLikeClass · 0.70
Foo2Class · 0.70
FooStrClass · 0.70
FloatSubclassClass · 0.70
MyIndexClass · 0.70
MyIntClass · 0.70
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected