mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
bpo-46611: add coverage to instance and class checks in typing.py
(GH-31078)
This commit is contained in:
parent
b556f53785
commit
067c03bf40
2 changed files with 59 additions and 16 deletions
|
@ -703,22 +703,62 @@ class UnionTests(unittest.TestCase):
|
||||||
self.assertEqual(hash(int | str), hash(str | int))
|
self.assertEqual(hash(int | str), hash(str | int))
|
||||||
self.assertEqual(hash(int | str), hash(typing.Union[int, str]))
|
self.assertEqual(hash(int | str), hash(typing.Union[int, str]))
|
||||||
|
|
||||||
def test_instancecheck(self):
|
def test_instancecheck_and_subclasscheck(self):
|
||||||
x = int | str
|
for x in (int | str, typing.Union[int, str]):
|
||||||
self.assertIsInstance(1, x)
|
with self.subTest(x=x):
|
||||||
self.assertIsInstance(True, x)
|
self.assertIsInstance(1, x)
|
||||||
self.assertIsInstance('a', x)
|
self.assertIsInstance(True, x)
|
||||||
self.assertNotIsInstance(None, x)
|
self.assertIsInstance('a', x)
|
||||||
self.assertTrue(issubclass(int, x))
|
self.assertNotIsInstance(None, x)
|
||||||
self.assertTrue(issubclass(bool, x))
|
self.assertTrue(issubclass(int, x))
|
||||||
self.assertTrue(issubclass(str, x))
|
self.assertTrue(issubclass(bool, x))
|
||||||
self.assertFalse(issubclass(type(None), x))
|
self.assertTrue(issubclass(str, x))
|
||||||
x = int | None
|
self.assertFalse(issubclass(type(None), x))
|
||||||
self.assertIsInstance(None, x)
|
|
||||||
self.assertTrue(issubclass(type(None), x))
|
for x in (int | None, typing.Union[int, None]):
|
||||||
x = int | collections.abc.Mapping
|
with self.subTest(x=x):
|
||||||
self.assertIsInstance({}, x)
|
self.assertIsInstance(None, x)
|
||||||
self.assertTrue(issubclass(dict, x))
|
self.assertTrue(issubclass(type(None), x))
|
||||||
|
|
||||||
|
for x in (
|
||||||
|
int | collections.abc.Mapping,
|
||||||
|
typing.Union[int, collections.abc.Mapping],
|
||||||
|
):
|
||||||
|
with self.subTest(x=x):
|
||||||
|
self.assertIsInstance({}, x)
|
||||||
|
self.assertNotIsInstance((), x)
|
||||||
|
self.assertTrue(issubclass(dict, x))
|
||||||
|
self.assertFalse(issubclass(list, x))
|
||||||
|
|
||||||
|
def test_instancecheck_and_subclasscheck_order(self):
|
||||||
|
T = typing.TypeVar('T')
|
||||||
|
|
||||||
|
will_resolve = (
|
||||||
|
int | T,
|
||||||
|
typing.Union[int, T],
|
||||||
|
)
|
||||||
|
for x in will_resolve:
|
||||||
|
with self.subTest(x=x):
|
||||||
|
self.assertIsInstance(1, x)
|
||||||
|
self.assertTrue(issubclass(int, x))
|
||||||
|
|
||||||
|
wont_resolve = (
|
||||||
|
T | int,
|
||||||
|
typing.Union[T, int],
|
||||||
|
)
|
||||||
|
for x in wont_resolve:
|
||||||
|
with self.subTest(x=x):
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
issubclass(int, x)
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
isinstance(1, x)
|
||||||
|
|
||||||
|
for x in (*will_resolve, *wont_resolve):
|
||||||
|
with self.subTest(x=x):
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
issubclass(object, x)
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
isinstance(object(), x)
|
||||||
|
|
||||||
def test_bad_instancecheck(self):
|
def test_bad_instancecheck(self):
|
||||||
class BadMeta(type):
|
class BadMeta(type):
|
||||||
|
|
|
@ -439,6 +439,8 @@ class TupleTests(BaseTestCase):
|
||||||
class MyTuple(tuple):
|
class MyTuple(tuple):
|
||||||
pass
|
pass
|
||||||
self.assertIsSubclass(MyTuple, Tuple)
|
self.assertIsSubclass(MyTuple, Tuple)
|
||||||
|
self.assertIsSubclass(Tuple, Tuple)
|
||||||
|
self.assertIsSubclass(tuple, Tuple)
|
||||||
|
|
||||||
def test_tuple_instance_type_error(self):
|
def test_tuple_instance_type_error(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
|
@ -466,6 +468,7 @@ class BaseCallableTests:
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
issubclass(types.FunctionType, Callable[[int], int])
|
issubclass(types.FunctionType, Callable[[int], int])
|
||||||
self.assertIsSubclass(types.FunctionType, Callable)
|
self.assertIsSubclass(types.FunctionType, Callable)
|
||||||
|
self.assertIsSubclass(Callable, Callable)
|
||||||
|
|
||||||
def test_eq_hash(self):
|
def test_eq_hash(self):
|
||||||
Callable = self.Callable
|
Callable = self.Callable
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue