Skip to content

Commit 40a9381

Browse files
authored
Fix B008 edge case with lambda functions (#243)
1 parent c255c1b commit 40a9381

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

bugbear.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,11 @@ def visit_Call(self, node):
791791
# Check for nested functions.
792792
self.generic_visit(node)
793793

794+
def visit_Lambda(self, node):
795+
# Don't recurse into lambda expressions
796+
# as they are evaluated at call time.
797+
pass
798+
794799
def visit(self, node):
795800
"""Like super-visit but supports iteration over lists."""
796801
self.arg_depth += 1

tests/b006_b008.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,8 @@ def no_nested_b006(a=map(lambda s: s.upper(), ["a", "b", "c"])):
180180
# B008-ception.
181181
def nested_b008(a=random.randint(0, dt.datetime.now().year)):
182182
pass
183+
184+
185+
# Ignore lambda contents since they are evaluated at call time.
186+
def foo(f=lambda x: print(x)):
187+
f(1)

tests/test_bugbear.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ def test_b006_b008(self):
124124
B008(170, 20),
125125
B008(170, 30),
126126
B008(176, 21),
127-
B008(176, 35),
128127
B008(181, 18),
129128
B008(181, 36),
130129
),

0 commit comments

Comments
 (0)