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

Conversation

merwok
Copy link
Member

@merwok merwok commented Jan 16, 2022

Parentheses are allowed in 3.10, so I did the minimal change to be accurate.

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 🙂

@gvanrossum gvanrossum merged commit c5f4d78 into main Jan 17, 2022
@gvanrossum gvanrossum deleted the pep8-with-statement-backslash branch January 17, 2022 05:44
aj-fuentes added a commit to odoo-dev/odoo that referenced this pull request Jan 22, 2024
Python3.7 and 3.8 do not support this syntax.
See: python/peps#2244
robodoo pushed a commit to odoo/odoo that referenced this pull request Jan 22, 2024
Python3.7 and 3.8 do not support this syntax.

See: python/peps#2244
Part-of: #150433
luanjubica pushed a commit to luanjubica/odoo-code that referenced this pull request Feb 14, 2024
Python3.7 and 3.8 do not support this syntax.

See: python/peps#2244
Part-of: odoo#150433
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.

7 participants