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 B008 edge case with lambda functions #243

Merged
merged 1 commit into from Mar 25, 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
5 changes: 5 additions & 0 deletions bugbear.py
Expand Up @@ -791,6 +791,11 @@ def visit_Call(self, node):
# Check for nested functions.
self.generic_visit(node)

def visit_Lambda(self, node):
# Don't recurse into lambda expressions
# as they are evaluated at call time.
pass

def visit(self, node):
"""Like super-visit but supports iteration over lists."""
self.arg_depth += 1
Expand Down
5 changes: 5 additions & 0 deletions tests/b006_b008.py
Expand Up @@ -180,3 +180,8 @@ def no_nested_b006(a=map(lambda s: s.upper(), ["a", "b", "c"])):
# B008-ception.
def nested_b008(a=random.randint(0, dt.datetime.now().year)):
pass


# Ignore lambda contents since they are evaluated at call time.
def foo(f=lambda x: print(x)):
f(1)
1 change: 0 additions & 1 deletion tests/test_bugbear.py
Expand Up @@ -124,7 +124,6 @@ def test_b006_b008(self):
B008(170, 20),
B008(170, 30),
B008(176, 21),
B008(176, 35),
B008(181, 18),
B008(181, 36),
),
Expand Down