Skip to content

gh-111673: Refactor test_float/complex.py (split out support classes) #110956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ddc670b
Refactor test_float/complex.py
serhiy-storchaka Jun 21, 2021
944de07
A continuation: factor out Complex/FloatLikeSubclass'es
skirpichev Oct 17, 2023
faf5d77
Merge branch 'main' into refactor-fc-tests
skirpichev Nov 4, 2023
0dedc80
Move support classes to Lib/test/support/classes.py
skirpichev Nov 4, 2023
aa0fe89
Rename MyIndex -> IndexLike, MyInt -> IntLike
skirpichev Nov 4, 2023
aeeba48
Refactor Lib/test/test_capi/test_long.py
skirpichev Nov 4, 2023
223c509
Redo Float/ComplexSubclass to return a different value from dunder me…
skirpichev Nov 4, 2023
bba21d1
Merge branch 'main' into refactor-fc-tests
skirpichev Nov 28, 2023
88e1624
Merge branch 'master' into refactor-fc-tests
skirpichev May 31, 2024
29c2862
Merge branch 'main' into refactor-fc-tests
skirpichev Jul 1, 2024
c2604af
+1
skirpichev Jul 1, 2024
09c8241
+ convert Lib/test/test_capi/test_complex.py
skirpichev Jul 1, 2024
dae5b2c
+ convert Lib/test/test_capi/test_float.py
skirpichev Jul 1, 2024
9830c25
Merge branch 'master' into refactor-fc-tests
skirpichev Jul 3, 2024
bfd4e36
+ convert Lib/test/test_capi/test_getargs.py
skirpichev Jul 3, 2024
139b646
Merge branch 'master' into refactor-fc-tests
skirpichev Jul 24, 2024
ca71582
Merge branch 'main' into refactor-fc-tests
skirpichev Sep 27, 2024
17cb3ab
Merge branch 'master' into refactor-fc-tests
skirpichev Apr 17, 2025
0ee08e3
Merge branch 'master' into refactor-fc-tests
skirpichev Apr 17, 2025
ed6a546
address review: classes.py -> number_helper.py
skirpichev Apr 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Lib/test/support/classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Common test classes.

class WithIndex:
def __init__(self, value):
self.value = value

def __index__(self):
return self.value


class IntSubclass(int):
pass

class WithInt:
def __init__(self, value):
self.value = value

def __int__(self):
return self.value

class WithIntAndIndex:
def __init__(self, value):
self.value = value

def __index__(self):
return self.value

def __int__(self):
return self.value + 12


class FloatSubclass(float):
pass

class OtherFloatSubclass(float):
pass

class WithFloat:
def __init__(self, value):
self.value = value

def __float__(self):
return self.value

class FloatLikeSubclass(float):
def __init__(self, value):
self.value = value

def __float__(self):
return self.value.__class__(self.value*2)


class ComplexSubclass(complex):
pass

class OtherComplexSubclass(complex):
pass

class WithComplex:
def __init__(self, value):
self.value = value

def __complex__(self):
return self.value

class ComplexLikeSubclass(complex):
def __init__(self, value):
self.value = value

def __complex__(self):
return self.value.__class__(self.value*2)
51 changes: 25 additions & 26 deletions Lib/test/test_capi/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import unittest
import warnings

from test.test_capi.test_getargs import (BadComplex, BadComplex2, Complex,
FloatSubclass, Float, BadFloat,
BadFloat2, ComplexSubclass)
from test.support import import_helper
from test.support.classes import (ComplexSubclass, FloatSubclass,
WithFloat, WithComplex)
from test.support.testcase import ComplexesAreIdenticalMixin


Expand All @@ -31,7 +30,7 @@ def test_check(self):

self.assertTrue(check(1+2j))
self.assertTrue(check(ComplexSubclass(1+2j)))
self.assertFalse(check(Complex()))
self.assertFalse(check(WithComplex(4.25+0.5j)))
self.assertFalse(check(3))
self.assertFalse(check(3.0))
self.assertFalse(check(object()))
Expand All @@ -44,7 +43,7 @@ def test_checkexact(self):

self.assertTrue(checkexact(1+2j))
self.assertFalse(checkexact(ComplexSubclass(1+2j)))
self.assertFalse(checkexact(Complex()))
self.assertFalse(checkexact(WithComplex(4.25+0.5j)))
self.assertFalse(checkexact(3))
self.assertFalse(checkexact(3.0))
self.assertFalse(checkexact(object()))
Expand Down Expand Up @@ -79,20 +78,20 @@ def test_realasdouble(self):
self.assertEqual(realasdouble(FloatSubclass(4.25)), 4.25)

# Test types with __complex__ dunder method
self.assertEqual(realasdouble(Complex()), 4.25)
self.assertRaises(TypeError, realasdouble, BadComplex())
self.assertEqual(realasdouble(WithComplex(4.25+0.5j)), 4.25)
self.assertRaises(TypeError, realasdouble, WithComplex(1.25))
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadComplex2()), 4.25)
self.assertEqual(realasdouble(WithComplex(ComplexSubclass(4.25+0.5j))), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, realasdouble, BadComplex2())
self.assertRaises(DeprecationWarning, realasdouble, WithComplex(ComplexSubclass(4.25+0.5j)))
self.assertRaises(RuntimeError, realasdouble, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(realasdouble(Float()), 4.25)
self.assertRaises(TypeError, realasdouble, BadFloat())
self.assertEqual(realasdouble(WithFloat(4.25)), 4.25)
self.assertRaises(TypeError, realasdouble, WithFloat(687))
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadFloat2()), 4.25)
self.assertEqual(realasdouble(WithFloat(FloatSubclass(4.25))), 4.25)

self.assertRaises(TypeError, realasdouble, object())

Expand All @@ -112,20 +111,20 @@ def test_imagasdouble(self):
self.assertEqual(imagasdouble(FloatSubclass(4.25)), 0.0)

# Test types with __complex__ dunder method
self.assertEqual(imagasdouble(Complex()), 0.5)
self.assertRaises(TypeError, imagasdouble, BadComplex())
self.assertEqual(imagasdouble(WithComplex(4.25+0.5j)), 0.5)
self.assertRaises(TypeError, imagasdouble, WithComplex(1.25))
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadComplex2()), 0.5)
self.assertEqual(imagasdouble(WithComplex(ComplexSubclass(4.25+0.5j))), 0.5)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, imagasdouble, BadComplex2())
self.assertRaises(DeprecationWarning, imagasdouble, WithComplex(ComplexSubclass(4.25+0.5j)))
self.assertRaises(RuntimeError, imagasdouble, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(imagasdouble(Float()), 0.0)
self.assertRaises(TypeError, imagasdouble, BadFloat())
self.assertEqual(imagasdouble(WithFloat(4.25)), 0.0)
self.assertRaises(TypeError, imagasdouble, WithFloat(687))
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadFloat2()), 0.0)
self.assertEqual(imagasdouble(WithFloat(FloatSubclass(4.25))), 0.0)

self.assertRaises(TypeError, imagasdouble, object())

Expand All @@ -147,20 +146,20 @@ def test_asccomplex(self):
self.assertEqual(asccomplex(FloatSubclass(4.25)), 4.25+0.0j)

# Test types with __complex__ dunder method
self.assertEqual(asccomplex(Complex()), 4.25+0.5j)
self.assertRaises(TypeError, asccomplex, BadComplex())
self.assertEqual(asccomplex(WithComplex(4.25+0.5j)), 4.25+0.5j)
self.assertRaises(TypeError, asccomplex, WithComplex(1.25))
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadComplex2()), 4.25+0.5j)
self.assertEqual(asccomplex(WithComplex(ComplexSubclass(4.25+0.5j))), 4.25+0.5j)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asccomplex, BadComplex2())
self.assertRaises(DeprecationWarning, asccomplex, WithComplex(ComplexSubclass(4.25+0.5j)))
self.assertRaises(RuntimeError, asccomplex, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(asccomplex(Float()), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, BadFloat())
self.assertEqual(asccomplex(WithFloat(4.25)), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, WithFloat(687))
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadFloat2()), 4.25+0.0j)
self.assertEqual(asccomplex(WithFloat(FloatSubclass(4.25))), 4.25+0.0j)

self.assertRaises(TypeError, asccomplex, object())

Expand Down
24 changes: 13 additions & 11 deletions Lib/test/test_capi/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import unittest
import warnings

from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2,
BadIndex2, BadFloat2, Index, BadIndex,
BadFloat)
from test.support import import_helper
from test.support.classes import FloatSubclass, WithIndex, WithFloat

_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
Expand All @@ -27,14 +25,18 @@
NAN = float("nan")


class FloatSubclass2(WithFloat, FloatSubclass):
pass


class CAPIFloatTest(unittest.TestCase):
def test_check(self):
# Test PyFloat_Check()
check = _testlimitedcapi.float_check

self.assertTrue(check(4.25))
self.assertTrue(check(FloatSubclass(4.25)))
self.assertFalse(check(Float()))
self.assertFalse(check(WithFloat(4.25)))
self.assertFalse(check(3))
self.assertFalse(check(object()))

Expand All @@ -46,7 +48,7 @@ def test_checkexact(self):

self.assertTrue(checkexact(4.25))
self.assertFalse(checkexact(FloatSubclass(4.25)))
self.assertFalse(checkexact(Float()))
self.assertFalse(checkexact(WithFloat(4.25)))
self.assertFalse(checkexact(3))
self.assertFalse(checkexact(object()))

Expand Down Expand Up @@ -93,18 +95,18 @@ def __float__(self):

self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25)
self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25)
self.assertEqual(asdouble(Index()), 99.)
self.assertEqual(asdouble(WithIndex(99)), 99.)

self.assertRaises(TypeError, asdouble, BadIndex())
self.assertRaises(TypeError, asdouble, BadFloat())
self.assertRaises(TypeError, asdouble, WithIndex(1.0))
self.assertRaises(TypeError, asdouble, WithFloat(687))
self.assertRaises(RuntimeError, asdouble, BadFloat3())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadIndex2()), 1.)
self.assertEqual(asdouble(WithIndex(True)), 1.)
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadFloat2()), 4.25)
self.assertEqual(asdouble(WithFloat(FloatSubclass(4.25))), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asdouble, BadFloat2())
self.assertRaises(DeprecationWarning, asdouble, WithFloat(FloatSubclass(4.25)))
self.assertRaises(TypeError, asdouble, object())
self.assertRaises(TypeError, asdouble, NULL)

Expand Down
Loading
Loading