From 40a93817aa089d56ab92dfe85dc014a8ca1e8e74 Mon Sep 17 00:00:00 2001 From: Joe Young <80432516+jpy-git@users.noreply.github.com> Date: Fri, 25 Mar 2022 16:53:21 +0000 Subject: [PATCH] Fix B008 edge case with lambda functions (#243) --- bugbear.py | 5 +++++ tests/b006_b008.py | 5 +++++ tests/test_bugbear.py | 1 - 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bugbear.py b/bugbear.py index 1c20197..1e281ec 100644 --- a/bugbear.py +++ b/bugbear.py @@ -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 diff --git a/tests/b006_b008.py b/tests/b006_b008.py index 781af9f..c5411c6 100644 --- a/tests/b006_b008.py +++ b/tests/b006_b008.py @@ -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) diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 9a61cef..d3ee22c 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -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), ),