Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B020: Fix comprehension false postives #238

Merged
merged 1 commit into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion bugbear.py
Expand Up @@ -557,7 +557,7 @@ def check_for_b020(self, node):
targets.visit(node.target)
ctrl_names = set(targets.names)

iterset = NameFinder()
iterset = B020NameFinder()
iterset.visit(node.iter)
iterset_names = set(iterset.names)

Expand Down Expand Up @@ -778,6 +778,22 @@ def visit(self, node):
return node


class B020NameFinder(NameFinder):
"""Ignore names defined within the local scope of a comprehension."""

def visit_GeneratorExp(self, node):
self.visit(node.generators)

def visit_ListComp(self, node):
self.visit(node.generators)

def visit_DictComp(self, node):
self.visit(node.generators)

def visit_comprehension(self, node):
self.visit(node.iter)


error = namedtuple("error", "lineno col message type vars")
Error = partial(partial, error, type=BugBearChecker, vars=())

Expand Down
17 changes: 16 additions & 1 deletion tests/b020.py
@@ -1,6 +1,6 @@
"""
Should emit:
B020 - on lines 8 and 21
B020 - on lines 8, 21, and 36
"""

items = [1, 2, 3]
Expand All @@ -20,3 +20,18 @@

for key, values in values.items():
print(f"{key}, {values}")

# Variables defined in a comprehension are local in scope
# to that comprehension and are therefore allowed.
for var in [var for var in range(10)]:
print(var)

for var in (var for var in range(10)):
print(var)

for k, v in {k: v for k, v in zip(range(10), range(10, 20))}.items():
print(k, v)

# However we still call out reassigning the iterable in the comprehension.
for vars in [i for i in vars]:
print(vars)
1 change: 1 addition & 0 deletions tests/test_bugbear.py
Expand Up @@ -284,6 +284,7 @@ def test_b020(self):
self.errors(
B020(8, 4, vars=("items",)),
B020(21, 9, vars=("values",)),
B020(36, 4, vars=("vars",)),
),
)

Expand Down