mirror of
https://github.com/python/cpython.git
synced 2025-08-10 11:58:39 +00:00
[3.12] gh-88834: Unify the instance check for typing.Union and types.UnionType (GH-128363) (GH-128371)
Union now uses the instance checks against its parameters instead of
the subclass checks.
(cherry picked from commit b2ac70a62a
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
fcc9bc49c4
commit
ca017a171a
3 changed files with 81 additions and 3 deletions
|
@ -121,7 +121,7 @@ class AnyTests(BaseTestCase):
|
|||
|
||||
def test_errors(self):
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(42, Any)
|
||||
isinstance(42, Any)
|
||||
with self.assertRaises(TypeError):
|
||||
Any[int] # Any is not a generic type.
|
||||
|
||||
|
@ -136,6 +136,9 @@ class AnyTests(BaseTestCase):
|
|||
|
||||
class MockSomething(Something, Mock): pass
|
||||
self.assertTrue(issubclass(MockSomething, Any))
|
||||
self.assertTrue(issubclass(MockSomething, MockSomething))
|
||||
self.assertTrue(issubclass(MockSomething, Something))
|
||||
self.assertTrue(issubclass(MockSomething, Mock))
|
||||
ms = MockSomething()
|
||||
self.assertIsInstance(ms, MockSomething)
|
||||
self.assertIsInstance(ms, Something)
|
||||
|
@ -1810,13 +1813,81 @@ class UnionTests(BaseTestCase):
|
|||
u = Union[int, float]
|
||||
self.assertNotEqual(u, Union)
|
||||
|
||||
def test_subclass_error(self):
|
||||
def test_union_isinstance(self):
|
||||
self.assertTrue(isinstance(42, Union[int, str]))
|
||||
self.assertTrue(isinstance('abc', Union[int, str]))
|
||||
self.assertFalse(isinstance(3.14, Union[int, str]))
|
||||
self.assertTrue(isinstance(42, Union[int, list[int]]))
|
||||
self.assertTrue(isinstance(42, Union[int, Any]))
|
||||
|
||||
def test_union_isinstance_type_error(self):
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Union[str, list[int]])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Union[list[int], int])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Union[list[int], str])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Union[str, Any])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Union[Any, int])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Union[Any, str])
|
||||
|
||||
def test_optional_isinstance(self):
|
||||
self.assertTrue(isinstance(42, Optional[int]))
|
||||
self.assertTrue(isinstance(None, Optional[int]))
|
||||
self.assertFalse(isinstance('abc', Optional[int]))
|
||||
|
||||
def test_optional_isinstance_type_error(self):
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Optional[list[int]])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(None, Optional[list[int]])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(42, Optional[Any])
|
||||
with self.assertRaises(TypeError):
|
||||
isinstance(None, Optional[Any])
|
||||
|
||||
def test_union_issubclass(self):
|
||||
self.assertTrue(issubclass(int, Union[int, str]))
|
||||
self.assertTrue(issubclass(str, Union[int, str]))
|
||||
self.assertFalse(issubclass(float, Union[int, str]))
|
||||
self.assertTrue(issubclass(int, Union[int, list[int]]))
|
||||
self.assertTrue(issubclass(int, Union[int, Any]))
|
||||
self.assertFalse(issubclass(int, Union[str, Any]))
|
||||
self.assertTrue(issubclass(int, Union[Any, int]))
|
||||
self.assertFalse(issubclass(int, Union[Any, str]))
|
||||
|
||||
def test_union_issubclass_type_error(self):
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(int, Union)
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(Union, int)
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(Union[int, str], int)
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(int, Union[str, list[int]])
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(int, Union[list[int], int])
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(int, Union[list[int], str])
|
||||
|
||||
def test_optional_issubclass(self):
|
||||
self.assertTrue(issubclass(int, Optional[int]))
|
||||
self.assertTrue(issubclass(type(None), Optional[int]))
|
||||
self.assertFalse(issubclass(str, Optional[int]))
|
||||
self.assertTrue(issubclass(Any, Optional[Any]))
|
||||
self.assertTrue(issubclass(type(None), Optional[Any]))
|
||||
self.assertFalse(issubclass(int, Optional[Any]))
|
||||
|
||||
def test_optional_issubclass_type_error(self):
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(list[int], Optional[list[int]])
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(type(None), Optional[list[int]])
|
||||
with self.assertRaises(TypeError):
|
||||
issubclass(int, Optional[list[int]])
|
||||
|
||||
def test_union_any(self):
|
||||
u = Union[Any]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue