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

F523: Single variable leads to literal rendering of newline #4826

Closed
Tracked by #4972
addisoncrump opened this issue Jun 3, 2023 · 4 comments · Fixed by #4883
Closed
Tracked by #4972

F523: Single variable leads to literal rendering of newline #4826

addisoncrump opened this issue Jun 3, 2023 · 4 comments · Fixed by #4883
Labels
bug Something isn't working

Comments

@addisoncrump
Copy link
Contributor

addisoncrump commented Jun 3, 2023

Reproduction

The following snippet is a minimal reproduction:

x = 5
"\n".format(x)

The fix attempted:

x = 5
"
".format(x)

Command line

ruff check format2.py --fix --isolated

Version information

Built on e82160a (latest, bleeding).

Misc

Discovered by #4822.

This is potentially a duplicate of #4823, but I couldn't diagnose a root cause. Notably, I chose to open a separate issue because, where #4823 causes an incorrect replacement of single quotes with double quotes, this issue literally renders the content of a string (!) and may have a separate root cause.

@charliermarsh charliermarsh added the bug Something isn't working label Jun 4, 2023
@charliermarsh
Copy link
Member

I think we should rewrite the F523 fix to use LibCST.

@dhruvmanila
Copy link
Member

This got fixed in d8a6109:

--- /Users/dhruv/playground/ruff/src/F523.py
+++ /Users/dhruv/playground/ruff/src/F523.py
@@ -1,2 +1,2 @@
 x = 5
-"\n".format(x)
+"\n".format()

Would fix 1 error.

@charliermarsh
Copy link
Member

I think it will still do the wrong thing in some cases, like if we need to rewrite the string to remove positionals, in which case we end up reconstructing from the AST.

@charliermarsh
Copy link
Member

Actually, I don't think we can use LibCST for this. It doesn't include a format-string parser. Instead, we probably need to change the format-string parser to include ranges on the various pieces, so that we can update the string in-place rather than recreate it.

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.

3 participants