Skip to content

unnecessary-lambda fix is unsafe even when there are no side-effects  #10663

@arrdem

Description

@arrdem
  • Ruff version: 0.3.3
  • Linter rule: unnecessary-lambda / PLW0108

Example

def foo(a, b):
  return a + b

def bar(c, d):
  return c, d

HANDLERS = {
  "foo": foo,
  "bar": lambda a, b: bar(a, b)  # "fixes" to just bar 
}

def handle(verb: str, payload):
  a, b = payload
  return HANDLERS[verb](a=a, b=b)

Here we're relying on the lambda to implement renaming of positional arguments. Removing the lambda expression would be incorrect despite an absence of side-effects. A correct refactor would be to rename the arguments to bar, or to eliminate the keyword invocation but the linter fix alone is unsafe despite listing itself as such.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions