From 4a069e5732938c496eb6cf708a284405fe2ba976 Mon Sep 17 00:00:00 2001 From: Sylvain MARIE Date: Tue, 16 Apr 2019 16:34:51 +0200 Subject: [PATCH] Fixed `NameError` in case of unknown symbols in type hints. Fixes #37 --- makefun/main.py | 2 ++ makefun/tests/_test_py35.py | 10 ++++++++++ makefun/tests/test_advanced.py | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/makefun/main.py b/makefun/main.py index 4886d23..bc982fb 100644 --- a/makefun/main.py +++ b/makefun/main.py @@ -310,6 +310,8 @@ def _signature_symbol_needs_protection(symbol, evaldict): try: deflt = eval(repr(symbol), evaldict) needs_protection = deflt != symbol + except NameError: + needs_protection = True except SyntaxError: needs_protection = True else: diff --git a/makefun/tests/_test_py35.py b/makefun/tests/_test_py35.py index 80144e0..6d1620d 100644 --- a/makefun/tests/_test_py35.py +++ b/makefun/tests/_test_py35.py @@ -22,3 +22,13 @@ def ref(a: A) -> A: pass return ref + + +def make_ref_function2(): + """ """ + from typing import Any + + def ref(a: Any): + pass + + return ref diff --git a/makefun/tests/test_advanced.py b/makefun/tests/test_advanced.py index 1d71da2..0ba65e1 100644 --- a/makefun/tests/test_advanced.py +++ b/makefun/tests/test_advanced.py @@ -141,3 +141,17 @@ def foo(a): return a assert foo(10) == 10 + + +@pytest.mark.skipif(sys.version_info < (3, 5), reason="requires python 3.5 or higher (non-comment type hints)") +def test_type_hint_error2(): + """ Test for https://github.com/smarie/python-makefun/issues/32 """ + + from makefun.tests._test_py35 import make_ref_function2 + ref_f = make_ref_function2() + + @wraps(ref_f) + def foo(a): + return a + + assert foo(10) == 10