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

Add documentation examples of consider-using-f-string #6460

Conversation

timmartin
Copy link
Contributor

Type of Changes

Type
📜 Docs

Description

Adds documentation examples for the consider-using-f-string checker. There didn't seem to be any examples in the pylint-errors repo that I could see, so I wrote a new one.

Ref #5953

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the documentation ! I think we need a detail.rst saying f-string performance at a lot better with a link to this tweet from a python core dev : https://twitter.com/raymondh/status/1205969258800275456. Also added more technical f-string with float formatting because often it's not known that this kind of thing can be done with fstring too

Comment on lines 3 to 5
order = "%s and %s" % menu # [consider-using-f-string]

new_order = "{1} and {0}".format(menu[0], menu[1]) # [consider-using-f-string]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
order = "%s and %s" % menu # [consider-using-f-string]
new_order = "{1} and {0}".format(menu[0], menu[1]) # [consider-using-f-string]
old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
beginner_order = menu[0] + "and " + menu[1] + ": " + str(menu[2]) + " ¤"
joined_order = "and ".join(menu)
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2]) # [consider-using-f-string]
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2]) # [consider-using-f-string]
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, thanks for introducing me to the ¤ sign!

@@ -0,0 +1,5 @@
menu = ('eggs', 'spam')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
menu = ('eggs', 'spam')
from string import Template
menu = ('eggs', 'spam', 42.42)

Comment on lines 3 to 5
order = f"{menu[0]} and {menu[1]}"

new_order = f"{menu[1]} and {menu[0]}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
order = f"{menu[0]} and {menu[1]}"
new_order = f"{menu[1]} and {menu[0]}"
f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @timmartin !

doc/data/messages/c/consider-using-f-string/details.rst Outdated Show resolved Hide resolved
@Pierre-Sassoulas Pierre-Sassoulas merged commit 8d608c9 into pylint-dev:main Apr 27, 2022
@Pierre-Sassoulas Pierre-Sassoulas removed this from the 2.14.0 milestone May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants