Skip to content

Commit c1ff4cb

Browse files
authored
[3.10] bpo-46611: add coverage to instance and class checks in typing.py (GH-31078) (GH-31182)
(cherry picked from commit 067c03b) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 3ceff99 commit c1ff4cb

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

Lib/test/test_types.py

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -695,22 +695,62 @@ def test_hash(self):
695695
self.assertEqual(hash(int | str), hash(str | int))
696696
self.assertEqual(hash(int | str), hash(typing.Union[int, str]))
697697

698-
def test_instancecheck(self):
699-
x = int | str
700-
self.assertIsInstance(1, x)
701-
self.assertIsInstance(True, x)
702-
self.assertIsInstance('a', x)
703-
self.assertNotIsInstance(None, x)
704-
self.assertTrue(issubclass(int, x))
705-
self.assertTrue(issubclass(bool, x))
706-
self.assertTrue(issubclass(str, x))
707-
self.assertFalse(issubclass(type(None), x))
708-
x = int | None
709-
self.assertIsInstance(None, x)
710-
self.assertTrue(issubclass(type(None), x))
711-
x = int | collections.abc.Mapping
712-
self.assertIsInstance({}, x)
713-
self.assertTrue(issubclass(dict, x))
698+
def test_instancecheck_and_subclasscheck(self):
699+
for x in (int | str, typing.Union[int, str]):
700+
with self.subTest(x=x):
701+
self.assertIsInstance(1, x)
702+
self.assertIsInstance(True, x)
703+
self.assertIsInstance('a', x)
704+
self.assertNotIsInstance(None, x)
705+
self.assertTrue(issubclass(int, x))
706+
self.assertTrue(issubclass(bool, x))
707+
self.assertTrue(issubclass(str, x))
708+
self.assertFalse(issubclass(type(None), x))
709+
710+
for x in (int | None, typing.Union[int, None]):
711+
with self.subTest(x=x):
712+
self.assertIsInstance(None, x)
713+
self.assertTrue(issubclass(type(None), x))
714+
715+
for x in (
716+
int | collections.abc.Mapping,
717+
typing.Union[int, collections.abc.Mapping],
718+
):
719+
with self.subTest(x=x):
720+
self.assertIsInstance({}, x)
721+
self.assertNotIsInstance((), x)
722+
self.assertTrue(issubclass(dict, x))
723+
self.assertFalse(issubclass(list, x))
724+
725+
def test_instancecheck_and_subclasscheck_order(self):
726+
T = typing.TypeVar('T')
727+
728+
will_resolve = (
729+
int | T,
730+
typing.Union[int, T],
731+
)
732+
for x in will_resolve:
733+
with self.subTest(x=x):
734+
self.assertIsInstance(1, x)
735+
self.assertTrue(issubclass(int, x))
736+
737+
wont_resolve = (
738+
T | int,
739+
typing.Union[T, int],
740+
)
741+
for x in wont_resolve:
742+
with self.subTest(x=x):
743+
with self.assertRaises(TypeError):
744+
issubclass(int, x)
745+
with self.assertRaises(TypeError):
746+
isinstance(1, x)
747+
748+
for x in (*will_resolve, *wont_resolve):
749+
with self.subTest(x=x):
750+
with self.assertRaises(TypeError):
751+
issubclass(object, x)
752+
with self.assertRaises(TypeError):
753+
isinstance(object(), x)
714754

715755
def test_bad_instancecheck(self):
716756
class BadMeta(type):

Lib/test/test_typing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ def test_tuple_subclass(self):
431431
class MyTuple(tuple):
432432
pass
433433
self.assertIsSubclass(MyTuple, Tuple)
434+
self.assertIsSubclass(Tuple, Tuple)
435+
self.assertIsSubclass(tuple, Tuple)
434436

435437
def test_tuple_instance_type_error(self):
436438
with self.assertRaises(TypeError):
@@ -458,6 +460,7 @@ def test_self_subclass(self):
458460
with self.assertRaises(TypeError):
459461
issubclass(types.FunctionType, Callable[[int], int])
460462
self.assertIsSubclass(types.FunctionType, Callable)
463+
self.assertIsSubclass(Callable, Callable)
461464

462465
def test_eq_hash(self):
463466
Callable = self.Callable

0 commit comments

Comments
 (0)