Skip to content

PLW0108 fix mishandles assignment expressions #18675

Closed
@dscorbett

Description

@dscorbett

Summary

The fix for unnecessary-lambda (PLW0108) introduces a syntax error when the callable in the top level of the lambda expression is an assignment expression. It should be parenthesized.

$ cat >plw0108_1.py <<'# EOF'
f = lambda x: (string := str)(x)
print(f(0))
# EOF

$ python plw0108_1.py
0

$ ruff --isolated check plw0108_1.py --select PLW0108 --preview --unsafe-fixes --diff 2>&1 | grep error:
error: Fix introduced a syntax error. Reverting all changes.

The fix can also change behavior when a variable bound by an assignment expression is one of the lambda expression’s parameters. The fix is already documented as unsafe because “the lambda body itself could contain an effect”, but in this case, the body is guaranteed to contain an effect, and there is no straightforward way to refactor it, so I recommend the fix be suppressed.

$ cat >plw0108_2.py <<'# EOF'
x = None
f = lambda x: ((x := 1) and str)(x)
print(f(0))
print(x)
# EOF

$ python plw0108_2.py
1
None

$ ruff --isolated check plw0108_2.py --select PLW0108 --preview --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ cat plw0108_2.py
x = None
f = (x := 1) and str
print(f(0))
print(x)

$ python plw0108_2.py
0
1

Version

ruff 0.11.13 (5faf72a 2025-06-05)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions