Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~
Expand Down
6 changes: 4 additions & 2 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/eval_files/b007.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 9 additions & 1 deletion tests/eval_files/b020.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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)