From 217fc52ef539e3ebc51208acf99e80d84de0b1dd Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Mon, 7 Oct 2024 15:58:54 -0700 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Jelle Zijlstra Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Alex Waygood --- peps/pep-0760.rst | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/peps/pep-0760.rst b/peps/pep-0760.rst index 905c840c78c..bccc280cffc 100644 --- a/peps/pep-0760.rst +++ b/peps/pep-0760.rst @@ -1,10 +1,9 @@ PEP: 760 Title: No More Bare Excepts Author: Pablo Galindo , Brett Cannon -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 -