Skip to content

Commit

Permalink
Fix crash when warning about annotated functions if function is objec…
Browse files Browse the repository at this point in the history
…t or partial function. (#93)
  • Loading branch information
jtbeach authored and agronholm committed Nov 15, 2019
1 parent 0341d72 commit f5c5ee9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/test_typeguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,24 @@ class Child(Parent):
assert Child.foo('bar') == 'Child'
pytest.raises(TypeError, Child.foo, 1)

def test_decorator_factory_no_annotations(self):
class CallableClass:
def __call__(self):
pass

def decorator_factory():
def decorator(f):
cmd = CallableClass()
return cmd

return decorator

with pytest.warns(UserWarning):
@typechecked
@decorator_factory()
def foo():
pass


class TestTypeChecker:
@pytest.fixture
Expand Down
5 changes: 3 additions & 2 deletions typeguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,17 @@ def qualified_name(obj) -> str:
return qualname if module in ('typing', 'builtins') else '{}.{}'.format(module, qualname)


def function_name(func: FunctionType) -> str:
def function_name(func: Callable) -> str:
"""
Return the qualified name of the given function.
Builtins and types from the :mod:`typing` package get special treatment by having the module
name stripped from the generated name.
"""
# For partial functions and objects with __call__ defined, __qualname__ does not exist
module = func.__module__
qualname = func.__qualname__
qualname = getattr(func, '__qualname__', repr(func))
return qualname if module == 'builtins' else '{}.{}'.format(module, qualname)


Expand Down

0 comments on commit f5c5ee9

Please sign in to comment.