-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
Run Flynt to convert str.format to f-strings #464
Conversation
Ran `flynt`: https://github.com/ikamensh/flynt against nox to automatically convert `"{}".format` style strings to the newer `f"{}"` style strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on converting the code base to f-strings, thanks!
Some comments:
- Sometimes expressions in the converted f-string contain a function call, or involve quotes. In these situations, it would be better for readability to extract the expression into a variable beforehand.
- I'm wondering why some calls of
str.format
in thenox
directory were not converted. Do you have any idea? - Can you include
noxfile.py
and the files in thetests
directory in the conversion? - As the diff shows, the
blacken
session insists on inserting the commas in unrelated files, even though it doesn't do so on main. Didn't investigate as it does not matter that much anyway.
I'm inclined to agree with you, I was going to do this but figured I'd see if the conversion was something you were interested in first. I can certainly fix this.
I'm not too sure, I ran
Yep can definitely do this!
The only thing I can think of is I just ran black directly from the command line (although it was the same version as the |
@cjolowicz I've been able to manually change most of the remaining I'm not too familiar with this style of formatting so I'm not entirely sure how to port this to f-strings (or if it's possible): if return_code not in success_codes:
logger.error(
"Command {} failed with exit code {}{}".format(
full_cmd, return_code, ":" if silent else ""
)
) There a few examples that follow the same pattern and a few in the tests that I could not convert e.g inside a parametrised test where replacing with an empty f-string caused a error (cannot have empty placeholder in f-string): @pytest.mark.parametrize("offline", [False, True], ids="offline={}".format) Aside from those I think I've got all of them. |
How about this? if return_code not in success_codes:
suffix = ":" if silent else ""
logger.error(f"Command {full_cmd} failed with exit code {return_code}{suffix}") The pytest example should be left unchanged, because |
Of course! The |
Is there no pyupgrade check? That should convert some (maybe not as many?) of these. Thanks, by the way, I heard about Flynt on Python Bytes, but didn't know how to spell it. :) |
Yeah looks like pyupgrade does this too! |
I could run |
I'm good with this PR as-is, @FollowTheProcess & @cjolowicz. Either of y'all can merge it if you'd like. |
Thanks guys! 🎉 |
I didn't file an Issue for this one because this is very much down to personal developer preference, so feel free to reject this if you don't want it I won't be offended at all! :)
Things like this would be very tedious to do by hand but I discovered an automated tool Flynt to convert the older style
"{}".format()
strings with newerf"{}"
style strings and it seems to have worked great!Since 3.5 is now EOL, it might make sense to convert to the new format!