Skip to content

Commit

Permalink
Fix B010 lambda flase positive (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpy-git committed Mar 29, 2022
1 parent e24eb10 commit d4afd14
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bugbear.py
Expand Up @@ -326,7 +326,8 @@ def visit_Call(self, node):
):
self.errors.append(B009(node.lineno, node.col_offset))
elif (
node.func.id == "setattr"
not any(isinstance(n, ast.Lambda) for n in self.node_stack)
and node.func.id == "setattr"
and len(node.args) == 3
and _is_identifier(node.args[1])
and not iskeyword(node.args[1].s)
Expand Down
19 changes: 18 additions & 1 deletion tests/b009_b010.py
@@ -1,6 +1,6 @@
"""
Should emit:
B009 - Line 17, 18, 19
B009 - Line 17, 18, 19, 44
B010 - Line 28, 29, 30
"""

Expand Down Expand Up @@ -28,3 +28,20 @@
setattr(foo, "bar", None)
setattr(foo, "_123abc", None)
setattr(foo, "abc123", None)

# Allow use of setattr within lambda expression
# since assignment is not valid in this context.
c = lambda x: setattr(x, "some_attr", 1)


class FakeCookieStore:
def __init__(self, has_setter):
self.cookie_filter = None
if has_setter:
self.setCookieFilter = lambda func: setattr(self, "cookie_filter", func)


# getattr is still flagged within lambda though
c = lambda x: getattr(x, "some_attr")
# should be replaced with
c = lambda x: x.some_attr
1 change: 1 addition & 0 deletions tests/test_bugbear.py
Expand Up @@ -169,6 +169,7 @@ def test_b009_b010(self):
B010(28, 0),
B010(29, 0),
B010(30, 0),
B009(45, 14),
]
self.assertEqual(errors, self.errors(*all_errors))

Expand Down

0 comments on commit d4afd14

Please sign in to comment.