Skip to content

Fixes for FURB122 and FURB142 should parenthesize lambdas and ternary expressions #18590

@dscorbett

Description

@dscorbett

Summary

The fixes for for-loop-writes (FURB122) and for-loop-set-mutations (FURB142) should parenthesize the iterable expression when it is a lambda expression or a ternary conditional expression and it is not already parenthesized and the fix produces a comprehension.

$ cat >furb122.py <<'# EOF'
from pathlib import Path
def write(lines_a, lines_b, *, write_a):
    with Path("file.txt").open("w", encoding="utf-8") as f:
        for line in lines_a if write_a else lines_b:
            f.write(f"[{line}]")
with Path("file.txt").open("w", encoding="utf-8") as f:
    for l in lambda: 0:
        f.write(f"[{l}]")
# EOF

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

$ cat >furb142.py <<'# EOF'
s = set()
for x in (1,) if True else (2,):
    s.add(-x)
for x in lambda: 0:
    s.discard(-x)
# EOF

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

Version

ruff 0.11.13 (5faf72a 2025-06-05)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions