Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W291: ruff --fix removes trailing whitespace in multiline string #8037

Closed
Jasha10 opened this issue Oct 18, 2023 · 1 comment · Fixed by #9744
Closed

W291: ruff --fix removes trailing whitespace in multiline string #8037

Jasha10 opened this issue Oct 18, 2023 · 1 comment · Fixed by #9744
Labels
bug Something isn't working

Comments

@Jasha10
Copy link

Jasha10 commented Oct 18, 2023

When W291 (trailing-whitespace) is enabled, ruff warns about trailing whitespace in multiline strings.
When --fix is passed, ruff modifies the multiline string. This could result in a behavior change of the python code.

Repro:

# repro.py
string = """
this line has trailing whitespace 
"""
$ ruff repro.py --isolated --select W291
repro.py:3:34: W291 [*] Trailing whitespace
Found 1 error.
[*] 1 potentially fixable with the --fix option.
$ ruff repro.py --isolated --select W291 --fix  # This modifies the multiline string!
Found 1 error (1 fixed, 0 remaining).
$ ruff --version
ruff 0.0.278

I noticed that the implementation trailing_whitespace categorizes the --fix action as Fix::safe_edit. I feel that modifying a multiline string is not safe; it can change the behavior of my python code. The doc for Applicability::Safe say:

    /// The fix is safe and can always be applied.
    /// The fix is definitely what the user intended, or it maintains the exact meaning of the code.

I feel that this does not apply to W291 as implemented.

@charliermarsh
Copy link
Member

Yeah, we should change to unsafe_edit when the diagnostic is inside a multiline string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants