Skip to content

Commit c95e36c

Browse files
committed
Docstrings shouldn't stop a function being a docstring.
Also, only having a docstring in a function is definitely a stub function.
1 parent 49ccd30 commit c95e36c

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

flake8_unused_arguments.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,15 @@ def is_stub_function(function: FunctionTypes) -> bool:
159159
if isinstance(function, ast.Lambda):
160160
return isinstance(function.body, ast.Ellipsis)
161161

162-
if (not isinstance(function, ast.Lambda)) and len(function.body) > 1:
163-
return False
164-
165162
statement = function.body[0]
163+
if isinstance(statement, ast.Expr) and isinstance(statement.value, ast.Str):
164+
if len(function.body) > 1:
165+
# first statement is a docstring, let's skip it
166+
statement = function.body[1]
167+
else:
168+
# it's a function with only a docstring, that's a stub
169+
return True
170+
166171
if isinstance(statement, ast.Pass):
167172
return True
168173
if isinstance(statement, ast.Expr) and isinstance(statement.value, ast.Ellipsis):

test_unused_arguments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def test_get_decorator_names(function, expected_result):
7777

7878

7979
@pytest.mark.parametrize("function, expected_result", [
80+
("def foo():\n 'with docstring'\n pass", True),
81+
("def foo():\n 'with docstring'", True),
82+
("def foo():\n 'with docstring'\n ...", True),
83+
("def foo():\n 'with docstring'\n return 5", False),
84+
("def foo():\n 'string' + 'with docstring'\n ...", False),
85+
("def foo():\n f = 'string' + 'with docstring'\n ...", False),
8086
("def foo(): pass", True),
8187
("def foo(): ...", True),
8288
("def foo(): return 5", False),

0 commit comments

Comments
 (0)