mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
Issue #26983: float() now always return an instance of exact float.
The deprecation warning is emitted if __float__ returns an instance of a strict subclass of float. In a future versions of Python this can be an error.
This commit is contained in:
parent
bb7f7327fe
commit
16931c3559
5 changed files with 76 additions and 32 deletions
|
@ -161,11 +161,12 @@ class GeneralFloatCases(unittest.TestCase):
|
|||
def __float__(self):
|
||||
return float(str(self)) + 1
|
||||
|
||||
self.assertAlmostEqual(float(Foo1()), 42.)
|
||||
self.assertAlmostEqual(float(Foo2()), 42.)
|
||||
self.assertAlmostEqual(float(Foo3(21)), 42.)
|
||||
self.assertEqual(float(Foo1()), 42.)
|
||||
self.assertEqual(float(Foo2()), 42.)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
self.assertEqual(float(Foo3(21)), 42.)
|
||||
self.assertRaises(TypeError, float, Foo4(42))
|
||||
self.assertAlmostEqual(float(FooStr('8')), 9.)
|
||||
self.assertEqual(float(FooStr('8')), 9.)
|
||||
|
||||
class Foo5:
|
||||
def __float__(self):
|
||||
|
@ -176,10 +177,14 @@ class GeneralFloatCases(unittest.TestCase):
|
|||
class F:
|
||||
def __float__(self):
|
||||
return OtherFloatSubclass(42.)
|
||||
self.assertAlmostEqual(float(F()), 42.)
|
||||
self.assertIs(type(float(F())), OtherFloatSubclass)
|
||||
self.assertAlmostEqual(FloatSubclass(F()), 42.)
|
||||
self.assertIs(type(FloatSubclass(F())), FloatSubclass)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
self.assertEqual(float(F()), 42.)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
self.assertIs(type(float(F())), float)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
self.assertEqual(FloatSubclass(F()), 42.)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
self.assertIs(type(FloatSubclass(F())), FloatSubclass)
|
||||
|
||||
def test_is_integer(self):
|
||||
self.assertFalse((1.1).is_integer())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue