-
Notifications
You must be signed in to change notification settings - Fork 2.4k
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
convert f-string with nothing to interpolate to regular string #1156
Comments
This can potentially be dangerous for f-strings with brackets. s = f"this can be safely transformed to a regular string"
s2 = f"Here we have {{double_brackets}}, should they be replaced with single? Who knows..." |
As I see from the source code, f-string parsing is very poor for now. Suggestion: what if we use
I have some drafts that could help. |
#1132 sets up the ground work for this. Here is an example of how long f-strings should be expected to behave after this pull request is merged: ##### INPUT
fstring = f"f-strings definitely make things more {difficult} than they need to be for {{black}}. But boy they sure are handy. The problem is that some lines will need to have the 'f' whereas others do not. This {line}, for example, needs one."
##### OUTPUT
fstring = (
f"f-strings definitely make things more {difficult} than they need to be for"
" {black}. But boy they sure are handy. The problem is that some lines will need"
f" to have the 'f' whereas others do not. This {line}, for example, needs one."
) As you can see, the I had thought about implementing this in a more general form (as this issue requests). Converting |
flake8 version 3.8.3 had error "F541 f-string is missing placeholders" that trips on exactly those kind of strings. While I understand the bright-line of "black doesn't change the AST", I have sort of gotten used to the crutch that "black will fix all the flake8 formatting problems" :) From an coder-in-the-editor perspective, removing the "f" from the middle string above feels about the same as removing it from a bare f"Hello". |
I'm OK with doing this; we already make known to be harmless AST changes in a few places. I believe on master we already remove useless fs in a few contexts. |
Wouldn't a linter be a better place for this type of transformation? Flake8 already flags this, but fails to offer an autofix - which IMO is why it's even considered for Black. If flake8 automatically fixed this for you, Black wouldn't need to. |
I feel like in general it's Black's job to make simple, safe formatting fixes. |
Half the time I forget to add the print(f"myvar=")
print("{myvar=}") The same argument applies to the "automatically add Sorry, but 👎 for me. |
Ruff can fix it automatically : https://beta.ruff.rs/docs/rules/f-string-missing-placeholders/ |
Is your feature request related to a problem? Please describe.
f-strings are great. sometimes they are so great we might use them even when we don't need to, such as in files where an f-string is used but nothing to interpolate was included in the string body.
Describe the solution you'd like
say you had a python script with a line like this:
black should see that f-string, recognize that it could just be a regular string and drop the f-prefix.
output:
Describe alternatives you've considered
Could return an error message, but I want black to format for me.
Could also not do this, but I think changing to a regular string is a better option.
The text was updated successfully, but these errors were encountered: