diff --git a/flake8_simplify/rules/ast_for.py b/flake8_simplify/rules/ast_for.py index 8d4d984..e19f64c 100644 --- a/flake8_simplify/rules/ast_for.py +++ b/flake8_simplify/rules/ast_for.py @@ -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( diff --git a/tests/test_100_rules.py b/tests/test_100_rules.py index fba08fe..955d71d 100644 --- a/tests/test_100_rules.py +++ b/tests/test_100_rules.py @@ -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: