Skip to content

Commit

Permalink
Fixed unwarranted TypeError when run within exec() (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyljones authored Nov 27, 2021
1 parent 4615f19 commit 580826d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/typeguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ def function_name(func: Callable) -> str:
"""
# For partial functions and objects with __call__ defined, __qualname__ does not exist
module = getattr(func, '__module__', '')
# For functions run in `exec` with a custom namespace, __module__ can be None
module = getattr(func, '__module__', '') or ''
qualname = (module + '.') if module not in ('builtins', '') else ''
return qualname + getattr(func, '__qualname__', repr(func))

Expand Down
13 changes: 13 additions & 0 deletions tests/test_typeguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def test_check_recursive_type():
r'List\[JSONType\], Dict\[str, JSONType\]\); got dict instead')


def test_exec_no_namespace():
from textwrap import dedent

exec(dedent("""
from typeguard import typechecked
@typechecked
def f():
pass
"""), {})


class TestCheckArgumentTypes:
def test_any_type(self):
def foo(a: Any):
Expand Down

0 comments on commit 580826d

Please sign in to comment.