bpo-44676: Add ability to serialize types.Union (GH-27244) · python/cpython@fe13f0b · GitHub
Skip to content

Commit fe13f0b

Browse files
authored
bpo-44676: Add ability to serialize types.Union (GH-27244)
1 parent 7d25254 commit fe13f0b

4 files changed

Lines changed: 90 additions & 2 deletions

File tree

Lib/test/test_types.py

Lines changed: 35 additions & 0 deletions

Lib/typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
329329
if isinstance(t, GenericAlias):
330330
return GenericAlias(t.__origin__, ev_args)
331331
if isinstance(t, types.Union):
332-
return functools.reduce(operator.or_, ev_args)
332+
return types.Union._from_args(ev_args)
333333
else:
334334
return t.copy_with(ev_args)
335335
return t
@@ -1808,7 +1808,7 @@ def _strip_annotations(t):
18081808
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
18091809
if stripped_args == t.__args__:
18101810
return t
1811-
return functools.reduce(operator.or_, stripped_args)
1811+
return types.Union._from_args(stripped_args)
18121812

18131813
return t
18141814

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ability to serialise ``types.Union`` objects. Patch provided by Yurii
2+
Karabas.

Objects/unionobject.c

Lines changed: 51 additions & 0 deletions

0 commit comments

Comments
 (0)