Skip to content

Commit 6a5f8b3

Browse files
author
Sylvain MARIE
committed
In python 2 some libraries such as attrs can modify the annotations manually, making signature return a string representation that is not compliant with the language version. This raised a SyntaxError in previous versions. The new version silently removes all these annotations in python versions that do not support them. Fixes #39
1 parent b1b728a commit 6a5f8b3

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

makefun/main.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,26 +271,34 @@ def get_signature_string(func_name, func_signature, evaldict):
271271
:param func_signature:
272272
:return:
273273
"""
274+
no_type_hints_allowed = sys.version_info < (3, 5)
275+
274276
# protect the parameters if needed
275277
new_params = []
276278
for p_name, p in func_signature.parameters.items():
277279
# if default value can not be evaluated, protect it
278280
default_needs_protection = _signature_symbol_needs_protection(p.default, evaldict)
279281
new_default = _protect_signature_symbol(p.default, default_needs_protection, "DEFAULT_%s" % p_name, evaldict)
280282

281-
# if type hint can not be evaluated, protect it
282-
annotation_needs_protection = _signature_symbol_needs_protection(p.annotation, evaldict)
283-
new_annotation = _protect_signature_symbol(p.annotation, annotation_needs_protection, "HINT_%s" % p_name,
284-
evaldict)
283+
if no_type_hints_allowed:
284+
new_annotation = Parameter.empty
285+
else:
286+
# if type hint can not be evaluated, protect it
287+
annotation_needs_protection = _signature_symbol_needs_protection(p.annotation, evaldict)
288+
new_annotation = _protect_signature_symbol(p.annotation, annotation_needs_protection, "HINT_%s" % p_name,
289+
evaldict)
285290

286291
# replace the parameter with the possibly new default and hint
287292
p = Parameter(p.name, kind=p.kind, default=new_default, annotation=new_annotation)
288293
new_params.append(p)
289294

290-
# if return type hint can not be evaluated, protect it
291-
return_needs_protection = _signature_symbol_needs_protection(func_signature.return_annotation, evaldict)
292-
new_return_annotation = _protect_signature_symbol(func_signature.return_annotation, return_needs_protection,
293-
"RETURNHINT", evaldict)
295+
if no_type_hints_allowed:
296+
new_return_annotation = Parameter.empty
297+
else:
298+
# if return type hint can not be evaluated, protect it
299+
return_needs_protection = _signature_symbol_needs_protection(func_signature.return_annotation, evaldict)
300+
new_return_annotation = _protect_signature_symbol(func_signature.return_annotation, return_needs_protection,
301+
"RETURNHINT", evaldict)
294302

295303
# copy signature object
296304
s = Signature(parameters=new_params, return_annotation=new_return_annotation)

makefun/tests/test_issues.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ def foo(a):
4949
pass
5050

5151

52+
def test_return_annotation_in_py2():
53+
"""Test for https://github.com/smarie/python-makefun/issues/39"""
54+
def f():
55+
pass
56+
57+
f.__annotations__ = {'return': None}
58+
59+
@wraps(f)
60+
def b():
61+
pass
62+
63+
b()
64+
65+
5266
def test_init_replaced():
5367

5468
class Foo(object):

0 commit comments

Comments
 (0)