Skip to content

Commit f186522

Browse files
committed
Check inner functions properly.
I thought that NodeVisitor was a little more recursive than this, but obviously not. Fixes nhoad#11.
1 parent 161eb00 commit f186522

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

flake8_unused_arguments.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,12 @@ def __init__(self) -> None:
198198
super().__init__()
199199
self.functions = []
200200

201-
def visit_AsyncFunctionDef(self, function: ast.AsyncFunctionDef) -> None:
202-
self.functions.append(function)
203-
204-
def visit_FunctionDef(self, function: ast.FunctionDef) -> None:
201+
def visit_function_types(self, function: FunctionTypes) -> None:
205202
self.functions.append(function)
203+
if isinstance(function, ast.Lambda):
204+
self.visit(function.body)
205+
else:
206+
for obj in function.body:
207+
self.visit(obj)
206208

207-
def visit_Lambda(self, function: ast.Lambda) -> None:
208-
self.functions.append(function)
209+
visit_AsyncFunctionDef = visit_FunctionDef = visit_Lambda = visit_function_types

test_unused_arguments.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,39 @@ def foo(cls, bar):
253253
{},
254254
[(3, 13, "U100 Unused argument 'bar'", "unused argument")],
255255
),
256+
(
257+
"""
258+
def cool(a):
259+
def inner(b):
260+
pass
261+
async def async_inner(c):
262+
pass
263+
async def async_cool(d):
264+
def inner(e):
265+
pass
266+
async def async_inner(f):
267+
pass
268+
""",
269+
{},
270+
[
271+
(2, 9, "U100 Unused argument 'a'", "unused argument"),
272+
(3, 14, "U100 Unused argument 'b'", "unused argument"),
273+
(5, 26, "U100 Unused argument 'c'", "unused argument"),
274+
(7, 21, "U100 Unused argument 'd'", "unused argument"),
275+
(8, 14, "U100 Unused argument 'e'", "unused argument"),
276+
(10, 26, "U100 Unused argument 'f'", "unused argument"),
277+
],
278+
),
279+
(
280+
"""
281+
# make sure we detect variables as used when they're referenced in an inner function
282+
def cool(a):
283+
def inner(c):
284+
a()
285+
""",
286+
{},
287+
[(4, 14, "U100 Unused argument 'c'", "unused argument")],
288+
),
256289
],
257290
)
258291
def test_integration(function, options, expected_warnings):

0 commit comments

Comments
 (0)