Skip to content

Commit

Permalink
Fixed AttributeError on callable non-method class members
Browse files Browse the repository at this point in the history
Fixes #90.
  • Loading branch information
agronholm committed Nov 10, 2019
1 parent 14e35b3 commit 70c29ef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-v
- Fixed ``@typechecked`` compatibility with built-in function wrappers
- Fixed type checking generator wrappers not being recognized as generators
- Fixed resolution of forward references in certain cases (inner classes, function-local classes)
- Fixed ``AttributeError`` when a class has contains a variable that is an instance of a class
that has a ``__call__()`` method

**2.6.0** (2019-11-06)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_typeguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,15 @@ async def create_inner(self) -> 'Inner':
retval = exc.value.value
assert isinstance(retval, LocalClass.Inner)

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

@typechecked
class LocalClass:
some_callable = CallableClass()


class TestTypeChecker:
@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions typeguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ class with this decorator.
prefix = func.__qualname__ + '.'
for key in dir(func):
attr = getattr(func, key, None)
if callable(attr) and attr.__qualname__.startswith(prefix):
if getattr(attr, '__annotations__', None):
if inspect.isfunction(attr) or inspect.ismethod(attr) or inspect.isclass(attr):
if attr.__qualname__.startswith(prefix) and getattr(attr, '__annotations__', None):
setattr(func, key, typechecked(attr, always=always, _localns=func.__dict__))

return func
Expand Down

0 comments on commit 70c29ef

Please sign in to comment.