PEP 8: update example of backslash usage for with statements#2244
PEP 8: update example of backslash usage for with statements#2244gvanrossum merged 1 commit intomainfrom
Conversation
| multiple ``with``-statements cannot use implicit continuation, so | ||
| backslashes are acceptable:: | ||
| multiple ``with``-statements could not use implicit continuation | ||
| before Python 3.10, so backslashes were acceptable for that case:: |
There was a problem hiding this comment.
Actually it works in CPython 3.9 too -- it was an easter egg of the PEG parser project.
There was a problem hiding this comment.
Ah, another dict-keys-are-sorted situation where it’s an implementation detail in one version and an official documented feature in the next. Not sure if I should weigh the text down with technical correctness here!
I’ll wait to see if someone proposes to replace the whole example with something else that still requires or looks better with backslashes.
There was a problem hiding this comment.
I feel like we should give an example with parenthesized with instead. The PEP should reflect the current preferred style, not the past style.
There was a problem hiding this comment.
The context is a section about backslashes, not context managers! This is saying that backslashes are generally discouraged, with some exceptions like multiple context managers, which now does not apply.
There was a problem hiding this comment.
From the perspective of a "regular" Python user, I suggest just picking whichever makes the more sense to tell users and go with that here.
There was a problem hiding this comment.
Since this section is about backslashes, what about just saying:
Backslashes may still be appropriate for cases where implicit continuation within parentheses.
the remove the with statement example. If you can't find another example that doesn't rely on with-statements, then enough said!
There was a problem hiding this comment.
If I understand correctly, this could potentially happen in any keyword statement with a syntax that incorporated commas to separate arguments that didn't explicitly allow commas:
returnandyieldstatements often have commas used within them, but that's because they are used with tuples, not have multiple distinct arguments, so they work fine- Except statements were fixed in Python 3
- As mentioned for
withstatements were formally fixed in Python 3.10, with an undocumented implementation-dependent fix in Python 3.9 importstatements, unlikefromstatements, don't allow parens for continuation, but that's deliberate to not encourage users to group too many top-level imports into the same import statement, and thus would be very anti-PEP 8 anyway- For assert statements, this is sorta true, at least without some gymnastics, as described in and proposed to be fixed by PEP 679
I think that's everything, though I'm just a regular Pythonista and not a BDFL or FLUFL like you folks, so I could easily have missed something :)
There was a problem hiding this comment.
Yeah, I'm just gonna merge it as-is.
There was a problem hiding this comment.
I wanted to see how it looks like with Adam’s suggestion (show recommended example first, in the paragraph just before that mentions parens, then keep backslash example with amended text), but I didn’t have time to do that immediately and the discussion did not wait until I came back 🙂
Python3.7 and 3.8 do not support this syntax. See: python/peps#2244
Python3.7 and 3.8 do not support this syntax. See: python/peps#2244 Part-of: #150433
Python3.7 and 3.8 do not support this syntax. See: python/peps#2244 Part-of: odoo#150433
Parentheses are allowed in 3.10, so I did the minimal change to be accurate.