summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-07-24 18:26:02 (GMT)
committerGitHub <noreply@github.com>2021-07-24 18:26:02 (GMT)
commit435a0334d341e5f8faed594d9f015746bb7845db (patch)
tree5c090299361ae9dabe002f2936044a01b1022b16 /Lib
parent4f5980a4f57dab68b9137304f58bd08891d43d5a (diff)
downloadcpython-435a0334d341e5f8faed594d9f015746bb7845db.zip
cpython-435a0334d341e5f8faed594d9f015746bb7845db.tar.gz
cpython-435a0334d341e5f8faed594d9f015746bb7845db.tar.bz2
bpo-44676: Serialize the union type using only public API (GH-27323)
Remove also the _from_args() constructor.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/copyreg.py6
-rw-r--r--Lib/test/test_types.py45
-rw-r--r--Lib/typing.py4
3 files changed, 23 insertions, 32 deletions
diff --git a/Lib/copyreg.py b/Lib/copyreg.py
index 7ab8c12..356db6f 100644
--- a/Lib/copyreg.py
+++ b/Lib/copyreg.py
@@ -36,6 +36,12 @@ else:
pickle(complex, pickle_complex, complex)
+def pickle_union(obj):
+ import functools, operator
+ return functools.reduce, (operator.or_, obj.__args__)
+
+pickle(type(int | str), pickle_union)
+
# Support for pickling new-style objects
def _reconstructor(cls, base, state):
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 3f491ee..b1218ab 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -3,6 +3,7 @@
from test.support import run_with_locale, cpython_only
import collections.abc
from collections import namedtuple
+import copy
import gc
import inspect
import pickle
@@ -807,36 +808,20 @@ class UnionTests(unittest.TestCase):
eq(x[S], int | S | bytes)
def test_union_pickle(self):
- alias = list[T] | int
- s = pickle.dumps(alias)
- loaded = pickle.loads(s)
- self.assertEqual(alias, loaded)
- self.assertEqual(alias.__args__, loaded.__args__)
- self.assertEqual(alias.__parameters__, loaded.__parameters__)
-
- def test_union_from_args(self):
- with self.assertRaisesRegex(
- TypeError,
- r"^Each union argument must be a type, got 1$",
- ):
- types.Union._from_args((1,))
-
- with self.assertRaisesRegex(
- TypeError,
- r"Union._from_args\(\) argument 'args' must be tuple, not int$",
- ):
- types.Union._from_args(1)
-
- with self.assertRaisesRegex(ValueError, r"args must be not empty"):
- types.Union._from_args(())
-
- alias = types.Union._from_args((int, list[T], None))
-
- self.assertEqual(alias.__args__, (int, list[T], type(None)))
- self.assertEqual(alias.__parameters__, (T,))
-
- result = types.Union._from_args((int,))
- self.assertIs(int, result)
+ orig = list[T] | int
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ s = pickle.dumps(orig, proto)
+ loaded = pickle.loads(s)
+ self.assertEqual(loaded, orig)
+ self.assertEqual(loaded.__args__, orig.__args__)
+ self.assertEqual(loaded.__parameters__, orig.__parameters__)
+
+ def test_union_copy(self):
+ orig = list[T] | int
+ for copied in (copy.copy(orig), copy.deepcopy(orig)):
+ self.assertEqual(copied, orig)
+ self.assertEqual(copied.__args__, orig.__args__)
+ self.assertEqual(copied.__parameters__, orig.__parameters__)
def test_union_parameter_substitution_errors(self):
T = typing.TypeVar("T")
diff --git a/Lib/typing.py b/Lib/typing.py
index 3403f55..844cf97 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -329,7 +329,7 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
if isinstance(t, GenericAlias):
return GenericAlias(t.__origin__, ev_args)
if isinstance(t, types.Union):
- return types.Union._from_args(ev_args)
+ return functools.reduce(operator.or_, ev_args)
else:
return t.copy_with(ev_args)
return t
@@ -1814,7 +1814,7 @@ def _strip_annotations(t):
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
if stripped_args == t.__args__:
return t
- return types.Union._from_args(stripped_args)
+ return functools.reduce(operator.or_, stripped_args)
return t