Skip to content

FURB122 fix fails on unparenthesized tuple #15936

@dscorbett

Description

@dscorbett

Description

Fixing for-loop-writes (FURB122) fails in Ruff 0.9.4 when the iterable expression is an unparenthesized tuple.

If the argument to write is the loop target, the modified code raises an error at runtime.

$ cat >furb122_1.py <<'# EOF'
with open("furb122.txt", "w") as f:
    for char in "a", "b":
        f.write(char)
# EOF

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

$ cat furb122_1.py
with open("furb122.txt", "w") as f:
    f.writelines("a", "b")

$ python furb122_1.py
Traceback (most recent call last):
  File "furb122_1.py", line 2, in <module>
    f.writelines("a", "b")
TypeError: _IOBase.writelines() takes exactly one argument (2 given)

Otherwise, the fix introduces a syntax error.

$ cat >furb122_2.py <<'# EOF'
with open("furb122.txt", "w") as f:
    for char in "a", "b":
        f.write(f"{char}")
# EOF

$ ruff --isolated check --preview --select FURB122 furb122_2.py --unsafe-fixes --diff

error: Fix introduced a syntax error. Reverting all changes.

This indicates a bug in Ruff. If you could open an issue at:

    https://github.com/astral-sh/ruff/issues/new?title=%5BFix%20error%5D

...quoting the contents of `furb122_2.py`, the rule codes FURB122, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

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