Skip to content

unnecessary-lambda (PLW0108)#

Derived from the Pylint linter.

Fix is always available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does#

Checks for lambda definitions that consist of a single function call with the same arguments as the lambda itself.

Why is this bad?#

When a lambda is used to wrap a function call, and merely propagates the lambda arguments to that function, it can typically be replaced with the function itself, removing a level of indirection.

Example#

df.apply(lambda x: str(x))

Use instead:

df.apply(str)

Fix safety#

This rule's fix is marked as unsafe in cases in which the lambda body itself contains an effect.

For example, replacing lambda x, y: (func()(x, y)) with func() would lead to a change in behavior, as func() would be evaluated eagerly when defining the lambda, rather than when the lambda is called.

When the lambda body contains no visible effects, the fix is considered safe.