diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2024-07-15 19:07:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-15 16:07:00 +0000 |
commit | 94bee45dee41876e88fe023b9163178d376355dc (patch) | |
tree | edbd2ba0cf02984c04d7691fbdb54857652d0840 /Lib/test/test_complex.py | |
parent | 8303d32ff55945c5b38eeeaf1b1811dbcf8aa9be (diff) | |
download | cpython-94bee45dee41876e88fe023b9163178d376355dc.tar.gz cpython-94bee45dee41876e88fe023b9163178d376355dc.zip |
gh-84978: Add float.from_number() and complex.from_number() (GH-26827)
They are alternate constructors which only accept numbers
(including objects with special methods __float__, __complex__
and __index__), but not strings.
Diffstat (limited to 'Lib/test/test_complex.py')
-rw-r--r-- | Lib/test/test_complex.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 155240e30f1..e3a2205c43d 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -36,6 +36,16 @@ class WithFloat: class ComplexSubclass(complex): pass +class OtherComplexSubclass(complex): + pass + +class MyInt: + def __init__(self, value): + self.value = value + + def __int__(self): + return self.value + class WithComplex: def __init__(self, value): self.value = value @@ -675,6 +685,35 @@ class ComplexTest(unittest.TestCase): if not any(ch in lit for ch in 'xXoObB'): self.assertRaises(ValueError, complex, lit) + def test_from_number(self, cls=complex): + def eq(actual, expected): + self.assertEqual(actual, expected) + self.assertIs(type(actual), cls) + + eq(cls.from_number(3.14), 3.14+0j) + eq(cls.from_number(3.14j), 3.14j) + eq(cls.from_number(314), 314.0+0j) + eq(cls.from_number(OtherComplexSubclass(3.14, 2.72)), 3.14+2.72j) + eq(cls.from_number(WithComplex(3.14+2.72j)), 3.14+2.72j) + eq(cls.from_number(WithFloat(3.14)), 3.14+0j) + eq(cls.from_number(WithIndex(314)), 314.0+0j) + + cNAN = complex(NAN, NAN) + x = cls.from_number(cNAN) + self.assertTrue(x != x) + self.assertIs(type(x), cls) + if cls is complex: + self.assertIs(cls.from_number(cNAN), cNAN) + + self.assertRaises(TypeError, cls.from_number, '3.14') + self.assertRaises(TypeError, cls.from_number, b'3.14') + self.assertRaises(TypeError, cls.from_number, MyInt(314)) + self.assertRaises(TypeError, cls.from_number, {}) + self.assertRaises(TypeError, cls.from_number) + + def test_from_number_subclass(self): + self.test_from_number(ComplexSubclass) + def test_hash(self): for x in range(-30, 30): self.assertEqual(hash(x), hash(complex(x, 0))) |