diff --git a/README.rst b/README.rst index 96e4978..d5def8a 100644 --- a/README.rst +++ b/README.rst @@ -692,6 +692,7 @@ UNRELEASED * B018, B031, B911: a function parameter named ``map``, ``groupby``, ``batched`` and so on means the bare name isn't the builtin/itertools one, like a local variable of that name already did +* B007, B020: also check ``async for`` loops 26.11.16 ~~~~~~~~ diff --git a/bugbear.py b/bugbear.py index e14f553..83ad03c 100644 --- a/bugbear.py +++ b/bugbear.py @@ -1215,6 +1215,8 @@ def visit_For(self, node: ast.For) -> None: def visit_AsyncFor(self, node: ast.AsyncFor) -> None: self.check_for_b045(node) + self.check_for_b007(node) + self.check_for_b020(node) self.check_for_b023(node) self.generic_visit(node) @@ -1537,7 +1539,7 @@ def check_for_b039(self, node: ast.Call) -> None: visitor.visit(kw.value) self.errors.extend(visitor.errors) - def check_for_b007(self, node: ast.For) -> None: + def check_for_b007(self, node: ast.For | ast.AsyncFor) -> None: targets = NameFinder() targets.visit(node.target) ctrl_names = set(filter(lambda s: not s.startswith("_"), targets.names)) @@ -1785,7 +1787,7 @@ def check_for_b049(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None: self.add_error("B049", abstract_decorator, path) return - def check_for_b020(self, node: ast.For) -> None: + def check_for_b020(self, node: ast.For | ast.AsyncFor) -> None: iterset = B020AttributeFinder() iterset.visit(node.iter) iterset_names = set(iterset.names) diff --git a/tests/eval_files/b007.py b/tests/eval_files/b007.py index e6dc7ef..c5c82fa 100644 --- a/tests/eval_files/b007.py +++ b/tests/eval_files/b007.py @@ -337,3 +337,11 @@ def f(): for gc in range(3): # B007: 8, "gc" f() + + +async def async_for_loops(xs): + async for i in xs: # B007: 14, "i" + print(10) + + async for j in xs: + print(j) diff --git a/tests/eval_files/b020.py b/tests/eval_files/b020.py index a624e8f..1d7db1f 100644 --- a/tests/eval_files/b020.py +++ b/tests/eval_files/b020.py @@ -1,6 +1,6 @@ """ Should emit: -B020 - on lines 8, 21, 36, 58, 75, and 103 +B020 - on lines 8, 21, 36, 58, 75, 103, and 108 """ items = [1, 2, 3] @@ -102,3 +102,11 @@ def ok_starred_nested_unpacking(items, value): def still_an_error_top_level_of_tuple(items): for key, items in list(items): # B020: 13, "items" print(key, items) + + +async def async_for_loops(items): + async for items in items: # B020: 14, "items" + print(items) + + async for item in items: + print(item)