8000 fix: #517 special handling of typing module · uqfoundation/dill@0ca195f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ca195f

Browse files
committed
fix: #517 special handling of typing module
1 parent 5a66152 commit 0ca195f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

dill/_dill.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def get_file_type(*args, **kwargs):
196196

197197
import inspect
198198
import dataclasses
199+
import typing
199200

200201
from pickle import GLOBAL
201202

@@ -761,6 +762,13 @@ def _create_ftype(ftypeobj, func, args, kwds):
761762
args = ()
762763
return ftypeobj(func, *args, **kwds)
763764

765+
def _create_typing_tuple(argz, *args): #NOTE: workaround python bug
766+
if not argz:
767+
return typing.Tuple[()].copy_with(())
768+
if argz == ((),):
769+
return typing.Tuple[()]
770+
return typing.Tuple[argz]
771+
764772
def _create_lock(locked, *args): #XXX: ignores 'blocking'
765773
from threading import Lock
766774
lock = Lock()
@@ -1276,6 +1284,23 @@ def save_classobj(pickler, obj): #FIXME: enable pickler._byref
12761284
logger.trace(pickler, "# C2")
12771285
return
12781286

1287+
@register(typing._GenericAlias)
1288+
def save_generic_alias(pickler, obj):
1289+
args = obj.__args__
1290+
if type(obj.__reduce__()) is str:
1291+
logger.trace(pickler, "Ga0: %s", obj)
10000
1292+
StockPickler.save_global(pickler, obj, name=obj.__reduce__())
1293+
logger.trace(pickler, "# Ga0")
1294+
elif obj.__origin__ is tuple and (not args or args == ((),)):
1295+
logger.trace(pickler, "Ga1: %s", obj)
1296+
pickler.save_reduce(_create_typing_tuple, (args,), obj=obj)
1297+
logger.trace(pickler, "# Ga1")
1298+
else:
1299+
logger.trace(pickler, "Ga2: %s", obj)
1300+
StockPickler.save_reduce(pickler, *obj.__reduce__(), obj=obj)
1301+
logger.trace(pickler, "# Ga2")
1302+
return
1303+
12791304
@register(LockType)
12801305
def save_lock(pickler, obj):
12811306
logger.trace(pickler, "Lo: %s", obj)

dill/tests/test_selected.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,26 @@ def test_frame_related():
9898
assert ok
9999
if verbose: print ("")
100100

101+
def test_typing():
102+
import typing #FIXME: test special cases
103+
x = typing.Dict[int, str]
104+
assert x == dill.copy(x)
105+
x = typing.List[int]
106+
assert x == dill.copy(x)
107+
x = typing.Tuple[int, str]
108+
assert x == dill.copy(x)
109+
x = typing.Tuple[int]
110+
assert x == dill.copy(x)
111+
x = typing.Tuple[()]
112+
assert x == dill.copy(x)
113+
x = typing.Tuple[()].copy_with(())
114+
assert x == dill.copy(x)
115+
return
116+
101117

102118
if __name__ == '__main__':
103119
test_frame_related()
104120
test_dict_contents()
105121
test_class()
106122
test_class_descriptors()
123+
test_typing()

0 commit comments

Comments
 (0)
0