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

Fix B010 lambda false positive #246

Merged
merged 1 commit into from Mar 29, 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
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:
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
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