[Python-checkins] [3.10] bpo-46611: add coverage to instance and class checks in `typing.py` (GH-31078) (GH-31182)
serhiy-storchaka
webhook-mailer at python.org
Mon Feb 7 03:48:43 EST 2022
https://github.com/python/cpython/commit/c1ff4cb98b11c00aee765019364544c71e7dd2df
commit: c1ff4cb98b11c00aee765019364544c71e7dd2df
branch: 3.10
author: Nikita Sobolev <mail at sobolevn.me>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-02-07T10:48:20+02:00
summary:
[3.10] bpo-46611: add coverage to instance and class checks in `typing.py` (GH-31078) (GH-31182)
(cherry picked from commit 067c03bf40d13393209f0138fa9c4d5980c4ff8a)
Co-authored-by: Nikita Sobolev <mail at sobolevn.me>
files:
M Lib/test/test_types.py
M Lib/test/test_typing.py
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 3dfda5cb95663..725d80d9ffdcf 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -695,22 +695,62 @@ def test_hash(self):
self.assertEqual(hash(int | str), hash(str | int))
self.assertEqual(hash(int | str), hash(typing.Union[int, str]))
- def test_instancecheck(self):
- x = int | str
- self.assertIsInstance(1, x)
- self.assertIsInstance(True, x)
- self.assertIsInstance('a', x)
- self.assertNotIsInstance(None, x)
- self.assertTrue(issubclass(int, x))
- self.assertTrue(issubclass(bool, x))
- self.assertTrue(issubclass(str, x))
- self.assertFalse(issubclass(type(None), x))
- x = int | None
- self.assertIsInstance(None, x)
- self.assertTrue(issubclass(type(None), x))
- x = int | collections.abc.Mapping
- self.assertIsInstance({}, x)
- self.assertTrue(issubclass(dict, x))
+ def test_instancecheck_and_subclasscheck(self):
+ for x in (int | str, typing.Union[int, str]):
+ with self.subTest(x=x):
+ self.assertIsInstance(1, x)
+ self.assertIsInstance(True, x)
+ self.assertIsInstance('a', x)
+ self.assertNotIsInstance(None, x)
+ self.assertTrue(issubclass(int, x))
+ self.assertTrue(issubclass(bool, x))
+ self.assertTrue(issubclass(str, x))
+ self.assertFalse(issubclass(type(None), x))
+
+ for x in (int | None, typing.Union[int, None]):
+ with self.subTest(x=x):
+ self.assertIsInstance(None, 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):
class BadMeta(type):
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 895805fb2a260..f13541e10d58c 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -431,6 +431,8 @@ def test_tuple_subclass(self):
class MyTuple(tuple):
pass
self.assertIsSubclass(MyTuple, Tuple)
+ self.assertIsSubclass(Tuple, Tuple)
+ self.assertIsSubclass(tuple, Tuple)
def test_tuple_instance_type_error(self):
with self.assertRaises(TypeError):
@@ -458,6 +460,7 @@ def test_self_subclass(self):
with self.assertRaises(TypeError):
issubclass(types.FunctionType, Callable[[int], int])
self.assertIsSubclass(types.FunctionType, Callable)
+ self.assertIsSubclass(Callable, Callable)
def test_eq_hash(self):
Callable = self.Callable
More information about the Python-checkins
mailing list