Skip to content

Docs: except with multiple exceptions parentheses not required#145848

Merged
nedbat merged 1 commit intopython:mainfrom
duilio:patch-1
Mar 13, 2026
Merged

Docs: except with multiple exceptions parentheses not required#145848
nedbat merged 1 commit intopython:mainfrom
duilio:patch-1

Conversation

@duilio
Copy link
Contributor

@duilio duilio commented Mar 12, 2026

As of PEP 758 the except statement doesn't require parentheses anymore for exception tuples.

See: https://peps.python.org/pep-0758/


📚 Documentation preview 📚: https://cpython-previews--145848.org.readthedocs.build/

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news labels Mar 12, 2026
@python-cla-bot
Copy link

python-cla-bot bot commented Mar 12, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I prefer the old style rather than the style personally, so I don't think we should remove one for the other. The entire library is till using the old style and we won't be switching to the new style as the old one is still fine and the most common.

For the tutorial, I'd prefer that we leave it as is (as otherwise, newcomers will be confused with existing code).

@picnixz picnixz closed this Mar 12, 2026
@picnixz picnixz reopened this Mar 12, 2026
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Sorry, I didn't want to close the PR. I wanted to ask for changes by telling that it is possible to use a non-parenthized form though I don't know if it has its place in the tutorial (should we advocate for multiple forms or not? the same can be said with multilines with statements where before we needed nested ones).

@bedevere-app
Copy link

bedevere-app bot commented Mar 12, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@picnixz
Copy link
Member

picnixz commented Mar 12, 2026

cc @hugovk @nedbat for their teaching's opinion and @pablogsal as the PEP-758's author

@nedbat
Copy link
Member

nedbat commented Mar 12, 2026

I would write:

An *except clause* may name multiple exceptions, for example::

   ... except RuntimeError, TypeError, NameError:
   ...     pass

The parentheses are not necessary, and "as a tuple" is distracting. The point of the paragraph is to show that multiple exceptions can be named, and this is the simplest way to show that.

Other code may use parentheses, but I don't think that will be a stumbling block. People are used to parentheses being optional for clarity, and there are bigger things people will need to understand as they progress from simple exception examples in the tutorial to real code.

@picnixz
Copy link
Member

picnixz commented Mar 12, 2026

I understand the reason and won't block on it

@bedevere-app
Copy link

bedevere-app bot commented Mar 12, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@hugovk hugovk added the needs backport to 3.14 bugs and security fixes label Mar 12, 2026
As of PEP 758 the except statement doesn't require parentheses anymore for exception tuples.

See: https://peps.python.org/pep-0758/
@duilio
Copy link
Contributor Author

duilio commented Mar 13, 2026

@picnixz I didn't want to make a style suggestion but to fix what I thought was a confusing statement: parentheses are not mandatory anymore. Just to give you a bit of context, I missed this change in 3.14 and ruff formatter insisted removing parentheses from my code and I thought it was a ruff bug. The first link I've found on Google was the tutorial page that confirmed my wrong assumption. Instead, the actual reference doc page is correct.

@nedbat I made the suggested change, it looks much better thanks.

@duilio duilio requested a review from nedbat March 13, 2026 08:35
@nedbat nedbat merged commit 6d1e9ce into python:main Mar 13, 2026
31 of 32 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Mar 13, 2026
@miss-islington-app
Copy link

Thanks @duilio for the PR, and @nedbat for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 13, 2026
…nGH-145848)

As of PEP 758 the except statement doesn't require parentheses anymore for exception tuples.
(cherry picked from commit 6d1e9ceed3e70ebc39953f5ad4f20702ffa32119)

Co-authored-by: Maurizio Sambati <duilio@users.noreply.github.com>
See: https://peps.python.org/pep-0758/
@bedevere-app
Copy link

bedevere-app bot commented Mar 13, 2026

GH-145904 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Mar 13, 2026
nedbat pushed a commit that referenced this pull request Mar 13, 2026
…GH-145848) (#145904)

Docs: except with multiple exceptions parentheses not required (GH-145848)

As of PEP 758 the except statement doesn't require parentheses anymore for exception tuples.
(cherry picked from commit 6d1e9ce)


See: https://peps.python.org/pep-0758/

Co-authored-by: Maurizio Sambati <duilio@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip issue skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants