-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Adam Turner <[email protected]>
- Loading branch information
Showing
1 changed file
with
27 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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: | ||
... | ||
|
@@ -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 | ||
|
@@ -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: | ||
... | ||
|
@@ -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): | ||
|
@@ -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 | ||
========= | ||
|