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

PEP 8: update example of backslash usage for with statements #2244

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pep-0008.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ parentheses. These should be used in preference to using a backslash
for line continuation.

Backslashes may still be appropriate at times. For example, long,
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::
Copy link
Member

Choose a reason for hiding this comment

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

Actually it works in CPython 3.9 too -- it was an easter egg of the PEG parser project.

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

I feel like we should give an example with parenthesized with instead. The PEP should reflect the current preferred style, not the past style.

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Member

Choose a reason for hiding this comment

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

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!

Copy link
Member

Choose a reason for hiding this comment

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

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:

  • return and yield statements 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 with statements were formally fixed in Python 3.10, with an undocumented implementation-dependent fix in Python 3.9
  • import statements, unlike from statements, 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 :)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I'm just gonna merge it as-is.

Copy link
Member Author

Choose a reason for hiding this comment

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

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 🙂


with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
Expand Down