From 9425bdfd9a4d1fcd004272eb52a5de3a67908f37 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Thu, 25 Jun 2026 18:40:48 +0200 Subject: [PATCH] Fix B020 false positive for attribute iterables --- bugbear.py | 12 ++++++++++++ tests/eval_files/b020.py | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/bugbear.py b/bugbear.py index e7ae9c9..cf814b4 100644 --- a/bugbear.py +++ b/bugbear.py @@ -2214,6 +2214,18 @@ def visit(self, node) -> None: class B020NameFinder(NameFinder): """Ignore names defined within the local scope of a comprehension.""" + def visit_Attribute(self, node) -> None: # noqa: B906 + pass + + def visit_Call(self, node) -> None: + if isinstance(node.func, ast.Attribute): + self.visit(node.func.value) + self.visit(node.args) + self.visit(node.keywords) + return + + self.generic_visit(node) + def visit_GeneratorExp(self, node) -> None: self.visit(node.generators) diff --git a/tests/eval_files/b020.py b/tests/eval_files/b020.py index e6b39ea..882d098 100644 --- a/tests/eval_files/b020.py +++ b/tests/eval_files/b020.py @@ -38,3 +38,8 @@ for var in sorted(range(10), key=lambda var: var.real): print(var) + +# Attribute access on the loop target name is not iterating over the same +# object, so it should not trigger B020. +for smoother in smoother.smoothers: + print(smoother)