Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/fixit/rules/no_redundant_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class NoRedundantLambda(LintRule):
Valid("lambda x, y: foo(y=x, x=y)"),
Valid("lambda x, y, *z: foo(x, y, z)"),
Valid("lambda x, y, **z: foo(x, y, z)"),
# The callee is re-evaluated on every call of the lambda, but only once
# if the lambda is replaced by it. See issue #509.
Valid("lambda: datetime.now().isoformat()"),
Valid("lambda x: get_handler().handle(x)"),
Valid("lambda x: foo()[0](x)"),
]
INVALID = [
Invalid("lambda: self.func()", expected_replacement="self.func"),
Expand Down Expand Up @@ -72,6 +77,15 @@ def visit_Lambda(self, node: cst.Lambda) -> None:
),
):
call = cst.ensure_type(node.body, cst.Call)

# Unwrapping is only safe while the callee itself is evaluated on
# every call. If it contains a call of its own, that call would be
# evaluated once, at definition time, and its result frozen:
# lambda: datetime.now().isoformat() -> datetime.now().isoformat
# See issue #509.
if m.findall(call.func, m.Call()):
return

full_name = get_full_name_for_node(call)
if full_name is None:
full_name = "function"
Expand Down