Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Support abc.abstractmethod in abstract detection #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ darglint = "darglint.driver:main"
"DAR" = "darglint.flake8_entry:DarglintChecker"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
requires = ["poetry_core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
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