-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Modernize email example from %-formatting to f-string #82532
Comments
A string was formatted with %s in the code example cpython/Doc/includes/email-simple.py Line 15 in b3e7045
It would be great to modernize that into fstring. Doc can be read at: https://docs.python.org/3.7/library/email.examples.html |
Does it make sense to change just one example? I'm not sure what the long-term stance is on whether %-formatting should be replaced at this point, but shouldn't this be a matter of which string formatting approach we want overall, rather than adjusting only specific examples? |
I think updating one isolated code example is less invasive and easier to review, instead of one big PR to change everything from "%s" to str.format or f-string. I brought up this example code merely because I happen to be reading this page. |
I agree that it's less invasive and easier to review. My question (and it's just that) is whether we've made a decision to prefer one formatting syntax over others (outside of examples discussing the formatting approaches themselves). If a decision is made to prefer one over others, it's worth making that decision separately, and then using separate PRs to deal with updates to different parts of the docs. Added Julien Palard to the issue; I'd value input on this. |
Welcome back from the OOOS break Mariatta!
I agree that we should reach a consensus on the preferred string formatting style. However, there seems to be two separate questions here:
The first one is far less costly and has very minimal risk of breakage. The cost of updating every *.py to use f-strings is worth considering, but is significantly higher and has more potential consequences, especially for the regression tests. I'm personally in favor of updating the code examples first and discussing the second question in a python-dev thread due to the wide impact.
Once we reach a decision on the matter, I think this particular issue could serve as a great first PR for a new contributor to become familiar with the workflow, so it would be a good candidate for the "newcomer friendly" label. Most python users are well acquainted with string formatting. I wouldn't mind opening a PR to fix it myself, but I think that leaving it open for a new contributor to work on as an intro to the workflow would be far more beneficial. Although there may be a benefit to use f-strings instead here, there's certainly no rush to have it completed in a short period of time. I would be in favor of having each PR address a single documentation file. This would help accelerate the review process and provide a valuable learning experience to a greater number of new contributors, in comparison to a single PR that updates every single code example in the docs. |
Never mind, just noticed this was already labeled as newcomer friendly. I only saw the "easy" label at first. (: If consensus is reached for this, we can open a separate issue for addressing the other doc files, something along the lines of "Update code examples to use f-strings". As mentioned earlier I think it would be worth having each new contributor update all of the instances in a single *.rst in a PR, but it can be a single issue. |
I definitely think we should not modify any code in the stdlib just to switch to f-strings. I think the code examples in the docs would benefit from a consistent style, and since f-strings are the least verbose way to format strings, I'd endorse using them except where .format or %-formatting is the point of the example. I agree with Fred that hearing from Julien would be helpful. |
Does this also apply to updating code to use f-strings in an area that's already being modified for a functional purpose? I agree that that we shouldn't update stdlib code for the sole purpose of switching to f-strings, but if a function or method is already being changed for another purpose, I don't think updating the formatting to use f-strings is an issue. This would probably have to be decided on a case-by-case basis though. |
No. As I said, not just to switch to f-strings. If other changes are also being made, discretion is advised. Just like any other cleanup. |
Definitely agree with Eric on this; code modernization is definitely on the risky side, so judicious updates are important. (Of course, not updating is also a risk, eventually. But not much of one in this case.) This issue is really about whether the docs should be updated to use the newer syntax. In general, I think we should update the docs, and we've delayed long enough for the general application of f-strings. There will be cases to be wary of, certainly. I'm thinking especially of calls to the logging methods, or anywhere else doing delayed formatting. (Not that I can think of others off the top of my head.) |
For the most part, templating examples can be switched to the .format() style but shouldn't be switched to f-strings. The former technique is still necessary if someone wants to move templates to an external file or if they need to use gettext() i18n, f-strings don't work well in the latter case. Also note, there are no plans to completely remove old-style formatting. AFAICT, it will be around forever, so people still need to see some examples of each. |
Is there no specific use case for the older "%s" % sub template that .format() doesn't have?
Interesting, I wasn't aware of that. This seems like it might be worth a brief mention in https://docs.python.org/3.9/library/stdtypes.html#str.format.
To allow users to see examples of each, would you suggest that we should leave the existing .format() examples as is and have more recently created examples use f-strings? I'd be okay with this, as long as there's a balance of both everywhere (particularly in the tutorial). Personally, I think that the f-string examples should be used more commonly used since they're generally more concise and readable. But, I can definitely understand the purpose of leaving the older ones around as long as they have a specific use case and are still utilized. |
I'd like to see consistent usage by default, with specific examples using the older forms as appropriate. The use cases Raymond identified are worth discussing (and the tutorial may be a good place for this), and well as mentioned in the reference docs for the '%s' % x and ''.format() operations (and string.Template(), of course). I would not like to see different syntaxes randomly applied to different examples that happen to involve formatting, but where that's not the emphasis. |
So what was decided? I can fix this issue and I can wait for a final conclusion as it wasn't clear from the thread. |
No decision yet. We're waiting for Julien's input. Thanks. |
Hi all, I'm a newcomer interested in making this small fix, but it looks like this has become a bit of a contentious issue. Are there any advances on whether this is a desirable fix? |
So, for newcomers finding this via "easy issues", go for it, you can fix this one :) On the different subjects discussed: # Mass-edit the stdlib We all agree that we should not mass-edit stdlib, (yet upgrading %-formatting to a newer syntax when modifying the same line may sometimes be a good idea). # Mass-edit the doc Having an f-string only doc is not possible, as Raymond mentionned (i18n, logging, templating), and not desirable: we need to document all existing formatting syntax. But the question is less "should we move everything to f-strings" than "should we move most examples out of %-formatting". # Let's not encourage %-formatting As we introduced str.format to fix issues from %-formatting [1] and allow extending formatting [2], we should not encourage newcomers to %-format strings. Good news: in the tutorial there's a *single* occurence of %-formatting in a paragraph named "Old string formatting"! There's probably a bunch of other places where upgrading the syntax in the doc would be a good idea, let's do it as we see them, when we feel it should obviously be upgraded, it also make nice easy issues, exactly as Mariatta did with this one (thanks Mariatta!). [1]: %-formatting a single value that may or may not be a tuple. |
Thanks, Julien! Sounds good to me; no reason for a PR addressing this specific issue to be held up once one becomes available. |
Thanks for the PR, Andrey! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: