Update test_types.py from CPython v3.11.2

This commit is contained in:
tdub0
2023-03-11 16:26:13 -07:00
parent 415cdb1ef9
commit 7be91fc92c

View File

@@ -527,18 +527,16 @@ class TypesTests(unittest.TestCase):
self.assertRaises(TypeError, 3.0.__format__, None)
self.assertRaises(TypeError, 3.0.__format__, 0)
# other format specifiers shouldn't work on floats,
# in particular int specifiers
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
if not format_spec in 'eEfFgGn%':
self.assertRaises(ValueError, format, 0.0, format_spec)
self.assertRaises(ValueError, format, 1.0, format_spec)
self.assertRaises(ValueError, format, -1.0, format_spec)
self.assertRaises(ValueError, format, 1e100, format_spec)
self.assertRaises(ValueError, format, -1e100, format_spec)
self.assertRaises(ValueError, format, 1e-100, format_spec)
self.assertRaises(ValueError, format, -1e-100, format_spec)
# confirm format options expected to fail on floats, such as integer
# presentation types
for format_spec in 'sbcdoxX':
self.assertRaises(ValueError, format, 0.0, format_spec)
self.assertRaises(ValueError, format, 1.0, format_spec)
self.assertRaises(ValueError, format, -1.0, format_spec)
self.assertRaises(ValueError, format, 1e100, format_spec)
self.assertRaises(ValueError, format, -1e100, format_spec)
self.assertRaises(ValueError, format, 1e-100, format_spec)
self.assertRaises(ValueError, format, -1e-100, format_spec)
# Alternate float formatting
test(1.0, '.0e', '1e+00')
@@ -604,6 +602,12 @@ class TypesTests(unittest.TestCase):
self.assertIsInstance(object.__lt__, types.WrapperDescriptorType)
self.assertIsInstance(int.__lt__, types.WrapperDescriptorType)
def test_dunder_get_signature(self):
sig = inspect.signature(object.__init__.__get__)
self.assertEqual(list(sig.parameters), ["instance", "owner"])
# gh-93021: Second parameter is optional
self.assertIs(sig.parameters["owner"].default, None)
def test_method_wrapper_types(self):
self.assertIsInstance(object().__init__, types.MethodWrapperType)
self.assertIsInstance(object().__str__, types.MethodWrapperType)
@@ -629,6 +633,14 @@ class TypesTests(unittest.TestCase):
def test_none_type(self):
self.assertIsInstance(None, types.NoneType)
def test_traceback_and_frame_types(self):
try:
raise OSError
except OSError as e:
exc = e
self.assertIsInstance(exc.__traceback__, types.TracebackType)
self.assertIsInstance(exc.__traceback__.tb_frame, types.FrameType)
class UnionTests(unittest.TestCase):
@@ -878,7 +890,7 @@ class UnionTests(unittest.TestCase):
T = typing.TypeVar("T")
x = int | T
with self.assertRaises(TypeError):
x[42]
x[int, str]
def test_or_type_operator_with_forward(self):
T = typing.TypeVar('T')
@@ -958,9 +970,9 @@ class UnionTests(unittest.TestCase):
with self.assertRaises(ZeroDivisionError):
list[int] | list[bt]
union_ga = (int | list[str], int | collections.abc.Callable[..., str],
int | d)
# Raise error when isinstance(type, type | genericalias)
union_ga = (list[str] | int, collections.abc.Callable[..., str] | int,
d | int)
# Raise error when isinstance(type, genericalias | type)
for type_ in union_ga:
with self.subTest(f"check isinstance/issubclass is invalid for {type_}"):
with self.assertRaises(TypeError):