Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
thejcannon committed Nov 23, 2021
1 parent abc26b7 commit fd37f93
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions darglint/analysis/analysis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ def _has_decorator(function, decorators):
decorators = (decorators,)

for decorator in function.decorator_list:
# Attributes (setters and getters) won't have an id.
if isinstance(decorator, ast.Name) and decorator.id in decorators:
decorator_name = None
if isinstance(decorator, ast.Name):
# Attributes (setters and getters) won't have an id.
decorator_name = decorator.id
elif isinstance(decorator, ast.Attribute):
decorator_name = decorator.attr

if decorator_name in decorators:
return True
return False
2 changes: 2 additions & 0 deletions tests/test_abstract_callable_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def check_abstract_decoration(self, program, result=True):
self.assertFalse(visitor.is_abstract)
visitor = self.analyzeAbstract("@abstractmethod\n" + reindent(program))
self.assertEqual(visitor.is_abstract, result)
visitor = self.analyzeAbstract("@abc.abstractmethod\n" + reindent(program))
self.assertEqual(visitor.is_abstract, result)

def check_abstract_toggle_doc(self, program, result=True, doc="None"):
self.check_abstract_decoration(program.format(docstring=""), result)
Expand Down
14 changes: 13 additions & 1 deletion tests/test_analysis_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _inner():
'''
self.assertFound(program, 'returns', [1], lambda x: 1)

def test_finds_abstract(self):
def test_finds_bare_abstractmethod(self):
program = r'''
@abstractmethod
def f(x):
Expand All @@ -86,6 +86,18 @@ def f(x):
visitor.visit(function)
self.assertTrue(visitor.is_abstract, 'Should have been marked abstract.')

def test_finds_attribute_abstractmethod(self):
program = r'''
@abc.abstractmethod
def f(x):
"""Halves the argument."""
pass
'''
function = ast.parse(reindent(program))
visitor = AnalysisVisitor()
visitor.visit(function)
self.assertTrue(visitor.is_abstract, 'Should have been marked abstract.')

def test_finds_not_abstract(self):
program = r'''
def f(x):
Expand Down

0 comments on commit fd37f93

Please sign in to comment.