Skip to content

Commit 196d32d

Browse files
committed
preserve annotations in canned functions
regression test for annotations, keyword-only defaults
1 parent 0635db7 commit 196d32d

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

ipyparallel/serialize/canning.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,15 @@ def __init__(self, f):
194194
self.defaults = None
195195

196196
if f.__kwdefaults__:
197-
self.kwdefaults = can(f.__kwdefaults__)
197+
self.kwdefaults = can_dict(f.__kwdefaults__)
198198
else:
199199
self.kwdefaults = None
200200

201+
if f.__annotations__:
202+
self.annotations = can_dict(f.__annotations__)
203+
else:
204+
self.annotations = None
205+
201206
closure = py3compat.get_closure(f)
202207
if closure:
203208
self.closure = tuple(can(cell) for cell in closure)
@@ -225,16 +230,23 @@ def get_object(self, g=None):
225230
defaults = None
226231

227232
if self.kwdefaults:
228-
kwdefaults = uncan(self.kwdefaults)
233+
kwdefaults = uncan_dict(self.kwdefaults)
229234
else:
230235
kwdefaults = None
236+
if self.annotations:
237+
annotations = uncan_dict(self.annotations)
238+
else:
239+
annotations = {}
231240

232241
if self.closure:
233242
closure = tuple(uncan(cell, g) for cell in self.closure)
234243
else:
235244
closure = None
236245
newFunc = FunctionType(self.code, g, self.__name__, defaults, closure)
237-
newFunc.__kwdefaults__ = kwdefaults
246+
if kwdefaults:
247+
newFunc.__kwdefaults__ = kwdefaults
248+
if annotations:
249+
newFunc.__annotations__ = annotations
238250
return newFunc
239251

240252

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)