-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: Hugo van Kemenade <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
- Loading branch information
1 parent
bba0c27
commit 217fc52
Showing
1 changed file
with
15 additions
and
17 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,10 +1,9 @@ | ||
PEP: 760 | ||
Title: No More Bare Excepts | ||
Author: Pablo Galindo <[email protected]>, Brett Cannon <[email protected]> | ||
PEP-Delegate: TBD | ||
Status: Draft | ||
Type: Standards Track | ||
Created: 30-Sep-2024 | ||
Created: 02-Oct-2024 | ||
Python-Version: 3.14 | ||
|
||
|
||
|
@@ -22,7 +21,7 @@ error handling. | |
Motivation | ||
========== | ||
|
||
The current syntax allows for catching all exceptions with a bare ``except:`` clause is: | ||
The current syntax allows for catching all exceptions with a bare ``except:`` clause: | ||
|
||
.. code-block:: python | ||
|
@@ -79,36 +78,36 @@ The syntax for the except clause will be modified to require an exception type. | |
The grammar will be updated to remove the possibility of adding an empty | ||
expression in except clauses. | ||
|
||
This change disallows the bare 'except:' syntax. All except clauses must | ||
This change disallows the bare ``except:`` syntax. All except clauses must | ||
specify at least one exception type: | ||
|
||
.. code-block:: python | ||
try: | ||
... | ||
except ValueError: | ||
... | ||
except (TypeError, RuntimeError): | ||
... | ||
except Exception: | ||
... # Still allowed, but catches all exceptions explicitly | ||
try: | ||
... | ||
except ValueError: | ||
... | ||
except (TypeError, RuntimeError): | ||
... | ||
except Exception: | ||
... # Still allowed, but catches all exceptions explicitly | ||
The semantics of exception handling remain unchanged, except that it will no | ||
longer be possible to catch all exceptions without explicitly specifying | ||
'Exception' or a similarly broad exception type. | ||
``BaseException`` or a similarly broad exception type. | ||
|
||
|
||
Backwards Compatibility | ||
======================= | ||
|
||
This change is not backwards compatible. Existing code that uses bare 'except:' | ||
This change is not backwards compatible. Existing code that uses bare ``except:`` | ||
clauses will need to be modified. To ease the transition: | ||
|
||
1. A deprecation warning will be issued for bare except clauses in Python 3.14. | ||
2. The syntax will be fully disallowed in Python 3.15. | ||
|
||
A tool will be provided to automatically update code to replace bare 'except:' | ||
with 'except Exception:'. | ||
A tool will be provided to automatically update code to replace bare ``except:`` | ||
with ``except BaseException:``. | ||
|
||
|
||
Security Implications | ||
|
@@ -159,4 +158,3 @@ CC0-1.0-Universal license, whichever is more permissive. | |
.. [6] https://4.docs.plone.org/develop/plone-coredev/style.html#concrete-rules | ||
.. [7] https://docs.openedx.org/en/latest/developers/references/developer_guide/style_guides/python-guidelines.html | ||