summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-17 21:10:21 (GMT)
committerGitHub <noreply@github.com>2021-07-17 21:10:21 (GMT)
commit03aad3049d1591c76a219dfe089e5367f88f167e (patch)
tree175acc35bac01a1cf8c7ef160096cc33f2e0064f /Lib
parent2d055ce13250a4074f66a945381a149a3cf8c46f (diff)
downloadcpython-03aad3049d1591c76a219dfe089e5367f88f167e.zip
cpython-03aad3049d1591c76a219dfe089e5367f88f167e.tar.gz
cpython-03aad3049d1591c76a219dfe089e5367f88f167e.tar.bz2
[3.10] bpo-44654: Refactor and clean up the union type implementation (GH-27196) (GH-27219)
(cherry picked from commit 0fd27375cabd12e68a2f12cfeca11a2d5043429e)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_types.py42
-rw-r--r--Lib/typing.py2
2 files changed, 26 insertions, 18 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 2d0e33f..b2e1130 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -611,6 +611,18 @@ class TypesTests(unittest.TestCase):
self.assertIsInstance(int.from_bytes, types.BuiltinMethodType)
self.assertIsInstance(int.__new__, types.BuiltinMethodType)
+ def test_ellipsis_type(self):
+ self.assertIsInstance(Ellipsis, types.EllipsisType)
+
+ def test_notimplemented_type(self):
+ self.assertIsInstance(NotImplemented, types.NotImplementedType)
+
+ def test_none_type(self):
+ self.assertIsInstance(None, types.NoneType)
+
+
+class UnionTests(unittest.TestCase):
+
def test_or_types_operator(self):
self.assertEqual(int | str, typing.Union[int, str])
self.assertNotEqual(int | list, typing.Union[int, str])
@@ -657,18 +669,23 @@ class TypesTests(unittest.TestCase):
with self.assertRaises(TypeError):
Example() | int
x = int | str
- self.assertNotEqual(x, {})
+ self.assertEqual(x, int | str)
+ self.assertEqual(x, str | int)
+ self.assertNotEqual(x, {}) # should not raise exception
with self.assertRaises(TypeError):
- (int | str) < typing.Union[str, int]
+ x < x
with self.assertRaises(TypeError):
- (int | str) < (int | bool)
+ x <= x
+ y = typing.Union[str, int]
with self.assertRaises(TypeError):
- (int | str) <= (int | str)
+ x < y
+ y = int | bool
with self.assertRaises(TypeError):
- # Check that we don't crash if typing.Union does not have a tuple in __args__
- x = typing.Union[str, int]
- x.__args__ = [str, int]
- (int | str ) == x
+ x < y
+ # Check that we don't crash if typing.Union does not have a tuple in __args__
+ y = typing.Union[str, int]
+ y.__args__ = [str, int]
+ self.assertEqual(x, y)
def test_hash(self):
self.assertEqual(hash(int | str), hash(str | int))
@@ -873,15 +890,6 @@ class TypesTests(unittest.TestCase):
self.assertLessEqual(sys.gettotalrefcount() - before, leeway,
msg='Check for union reference leak.')
- def test_ellipsis_type(self):
- self.assertIsInstance(Ellipsis, types.EllipsisType)
-
- def test_notimplemented_type(self):
- self.assertIsInstance(NotImplemented, types.NotImplementedType)
-
- def test_none_type(self):
- self.assertIsInstance(None, types.NoneType)
-
class MappingProxyTests(unittest.TestCase):
mappingproxy = types.MappingProxyType
diff --git a/Lib/typing.py b/Lib/typing.py
index 1823cb8..508f4b6 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1181,7 +1181,7 @@ class _UnionGenericAlias(_GenericAlias, _root=True):
return Union[params]
def __eq__(self, other):
- if not isinstance(other, _UnionGenericAlias):
+ if not isinstance(other, (_UnionGenericAlias, types.Union)):
return NotImplemented
return set(self.__args__) == set(other.__args__)