Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Turner <[email protected]>
  • Loading branch information
pablogsal and AA-Turner authored Sep 30, 2024
1 parent 26b41f7 commit 5f9ec8c
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions peps/pep-0790.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PEP: 790
Title: Removing the parentheses requirement from ``except`` expressions
Title: Allow ``except`` expressions without parentheses
Author: Pablo Galindo <[email protected]>, Brett Cannon <[email protected]>
PEP-Delegate: TBD
Discussions-To: xxxx
Expand All @@ -18,14 +18,18 @@ This PEP [1]_ proposes to allow unparenthesized ``except`` blocks in Python's ex
Motivation
==========

The current syntax for catching multiple exceptions requires parentheses in the ``except`` expression: ::
The current syntax for catching multiple exceptions requires parentheses in the ``except`` expression:

.. code-block:: python
try:
...
except (ExceptionA, ExceptionB, ExceptionC):
...
While this syntax is clear and unambiguous, it can be seen as unnecessarily verbose in some cases, especially when catching a large number of exceptions. By allowing the omission of parentheses, we can simplify the syntax: ::
While this syntax is clear and unambiguous, it can be seen as unnecessarily verbose in some cases, especially when catching a large number of exceptions. By allowing the omission of parentheses, we can simplify the syntax:

.. code-block:: python
try:
...
Expand All @@ -49,7 +53,9 @@ The decision to allow unparenthesized ``except`` blocks is based on the followin
Specification
=============

The syntax for the except clause will be modified to allow an unparenthesized list of exception types. The grammar will be updated as follows: ::
The syntax for the except clause will be modified to allow an unparenthesized list of exception types. The grammar will be updated as follows:

.. code-block:: peg
except_block[excepthandler_ty]:
| invalid_except_stmt_indent
Expand All @@ -64,7 +70,9 @@ The syntax for the except clause will be modified to allow an unparenthesized li
| invalid_except_star_stmt
This allows both the current parenthesized syntax and the new unparenthesized syntax: ::
This allows both the current parenthesized syntax and the new unparenthesized syntax:

.. code-block:: python
try:
...
Expand All @@ -91,14 +99,18 @@ There are no known security implications for this change. The semantics of excep
How to Teach This
=================

For new Python users, the unparenthesized syntax can be taught as the standard way to catch multiple exceptions: ::
For new Python users, the unparenthesized syntax can be taught as the standard way to catch multiple exceptions:

.. code-block:: python
try:
risky_operation()
except ValueError, TypeError, OSError:
handle_errors()
For experienced users, it can be introduced as a new, optional syntax that can be used interchangeably with the parenthesized version. Documentation should note that both forms are equivalent: ::
For experienced users, it can be introduced as a new, optional syntax that can be used interchangeably with the parenthesized version. Documentation should note that both forms are equivalent:

.. code-block:: python
# These are equivalent:
except (ValueError, TypeError):
Expand All @@ -119,11 +131,16 @@ A proof-of-concept implementation is available at https://github.com/pablogsal/c
Rejected Ideas
==============

1. Allowing mixed parenthesized and unparenthesized syntax: ::
1. Allowing mixed parenthesized and unparenthesized syntax:

.. code-block:: python
except (ValueError, TypeError), OSError:
try:
...
except (ValueError, TypeError), OSError:
...
This was rejected due to the potential for confusion and to maintain a clear distinction between the two styles.
This was rejected due to the potential for confusion and to maintain a clear distinction between the two styles.

Footnotes
=========
Expand Down

0 comments on commit 5f9ec8c

Please sign in to comment.