Skip to content

Commit

Permalink
SIM104: Remove false-positives in case the loop is not a direct child…
Browse files Browse the repository at this point in the history
… of an async function (#147)

Closes #146
  • Loading branch information
wyuenho authored Jul 28, 2022
1 parent f01e09f commit 381fff5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion flake8_simplify/rules/ast_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ def get_sim104(node: ast.For) -> List[Tuple[int, int, str]]:
or node.orelse != []
):
return errors
if isinstance(node.parent, ast.AsyncFunctionDef): # type: ignore

parent = getattr(node, "parent", None)
while (parent
and hasattr(parent, "parent")
and parent.parent is not parent
and not isinstance(parent, ast.AsyncFunctionDef)):
parent = getattr(parent, "parent", None)

if isinstance(parent, ast.AsyncFunctionDef): # type: ignore
return errors
iterable = to_source(node.iter)
errors.append(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_100_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ def test_s104_async_generator_false_positive():
assert "SIM104" not in el


def test_s104_async_generator_false_positive_2():
ret = _results(
"""async def items():
with open('/etc/passwd') as f:
for line in f:
yield line"""
)
for el in ret:
assert "SIM104" not in el


def test_sim105():
ret = _results(
"""try:
Expand Down

0 comments on commit 381fff5

Please sign in to comment.