Skip to content
Merged
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
12 changes: 11 additions & 1 deletion auto_type_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def _args(node: ast.AsyncFunctionDef | ast.FunctionDef) -> Generator[ast.arg]:
yield subnode


def _is_abstract(node: ast.AST) -> bool:
return (
isinstance(node, ast.Attribute) and node.attr == 'abstractmethod' or
isinstance(node, ast.Name) and node.id == 'abstractmethod'
)


class FindUntyped(ast.NodeVisitor):
def __init__(self) -> None:
self._mod: list[Mod] = []
Expand All @@ -73,7 +80,10 @@ def visit_FunctionDef(
self,
node: ast.AsyncFunctionDef | ast.FunctionDef,
) -> None:
if not self._in_func[-1]:
if (
not self._in_func[-1] and
not any(_is_abstract(dec) for dec in node.decorator_list)
):
args = tuple(_args(node))
if node.name == '__init__' and len(args) > 1:
missing_annotation = False
Expand Down
22 changes: 22 additions & 0 deletions tests/auto_type_annotate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ def test_find_untyped_async_def():
assert _find_untyped(src) == [(_MOD, 'f')]


def test_find_untyped_skips_abstract():
src = '''\
import abc

class C:
@abc.abstractmethod
def f(self): pass
'''
assert _find_untyped(src) == []


def test_find_untyped_skips_abstract_from_imported():
src = '''\
from abc import abstractmethod

class C:
@abstractmethod
def f(self): pass
'''
assert _find_untyped(src) == []


@contextlib.contextmanager
def _dmypy():
subprocess.check_call((sys.executable, '-m', 'mypy.dmypy', 'run', '.'))
Expand Down