Skip to content

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

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)

Activity

  1. added
    bugAn issue describing something that isn't working, or a PR that fixes a bug
    fixesRelated to suggested fixes for violations
    on Jun 9, 2025
  2. added 2 commits that reference this issue on Jun 9, 2025
    9315772
    79006df
  3. added a commit that references this issue on Jun 10, 2025
    828b35f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue describing something that isn't working, or a PR that fixes a bugfixesRelated 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