Skip to content

RET504 fix uses the equals sign from a comment #18411

@dscorbett

Description

@dscorbett

Summary

The fix for unnecessary-assign (RET504) assumes that the first equals sign in the assignment statement is the one that does the assignment. However, if the first equals sign is in a comment, the fix can fail because of a syntax error. The fix should seek the first equals sign token, not the first equals sign character.

$ cat >ret504.py <<'# EOF'
def f():
    (#=
    x) = 1
    return x
# EOF

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

Version

ruff 0.11.12 (aee3af0 2025-05-29)

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