Skip to content

Commit 5508ec3

Browse files
authored
Merge pull request #465 from minrk/keyword-defaults
preserve keyword defaults, annotations
2 parents f06532c + 196d32d commit 5508ec3

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

ipyparallel/serialize/canning.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ def __init__(self, f):
193193
else:
194194
self.defaults = None
195195

196+
if f.__kwdefaults__:
197+
self.kwdefaults = can_dict(f.__kwdefaults__)
198+
else:
199+
self.kwdefaults = None
200+
201+
if f.__annotations__:
202+
self.annotations = can_dict(f.__annotations__)
203+
else:
204+
self.annotations = None
205+
196206
closure = py3compat.get_closure(f)
197207
if closure:
198208
self.closure = tuple(can(cell) for cell in closure)
@@ -218,11 +228,25 @@ def get_object(self, g=None):
218228
defaults = tuple(uncan(cfd, g) for cfd in self.defaults)
219229
else:
220230
defaults = None
231+
232+
if self.kwdefaults:
233+
kwdefaults = uncan_dict(self.kwdefaults)
234+
else:
235+
kwdefaults = None
236+
if self.annotations:
237+
annotations = uncan_dict(self.annotations)
238+
else:
239+
annotations = {}
240+
221241
if self.closure:
222242
closure = tuple(uncan(cell, g) for cell in self.closure)
223243
else:
224244
closure = None
225245
newFunc = FunctionType(self.code, g, self.__name__, defaults, closure)
246+
if kwdefaults:
247+
newFunc.__kwdefaults__ = kwdefaults
248+
if annotations:
249+
newFunc.__annotations__ = annotations
226250
return newFunc
227251

228252

ipyparallel/tests/test_canning.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def loads(obj):
2121
return uncan(pickle.loads(obj))
2222

2323

24+
def roundtrip(obj):
25+
return loads(dumps(obj))
26+
27+
2428
def test_no_closure():
2529
@interactive
2630
def foo():
@@ -109,3 +113,20 @@ def foo(arg1, arg2, kwarg1, kwarg2):
109113
loaded.buffers = buffers
110114
pfoo2 = uncan(loaded)
111115
assert pfoo2() == partial_foo()
116+
117+
118+
def test_keyword_only_arguments():
119+
def compute(a, *, b=3):
120+
return a * b
121+
122+
assert compute(2) == 6
123+
compute_2 = roundtrip(compute)
124+
assert compute_2(2) == 6
125+
126+
127+
def test_annotations():
128+
def f(a: int):
129+
return a
130+
131+
f2 = roundtrip(f)
132+
assert f2.__annotations__ == f.__annotations__

0 commit comments

Comments
 (0)