Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP-654: add BaseExceptionGroup//ExceptionGroup split #1850

Merged
merged 3 commits into from
Mar 1, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions pep-0654.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,24 @@ Specification
ExceptionGroup
--------------

The new builtin exception type, ``ExceptionGroup`` is a subclass of
``BaseException``, so it is assignable to ``Exception.__cause__`` and
``Exception.__context__``, and can be raised and handled as any exception
with ``raise ExceptionGroup(...)`` and ``try: ... except ExceptionGroup: ...``.

Its constructor takes two positional-only parameters: a message string and a
sequence of the nested exceptions, for example:
We propose to add two new builtin exception types: ``ExceptionGroup`` and
``BaseExceptionGroup``. The former is a subclass of ``Exception`` and
the latter is a subclass of ``BaseException``. Both are assignable to
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
``Exception.__cause__`` and ``Exception.__context__``, and they can be
raised and handled as any exception with ``raise ExceptionGroup(...)`` and
``try: ... except ExceptionGroup: ...`` or ``raise BaseExceptionGroup(...)``
and ``try: ... except BaseExceptionGroup: ...``.

Both have a constructor that takes two positional-only parameters: a message
string and a sequence of the nested exceptions, for example:
``ExceptionGroup('issues', [ValueError('bad value'), TypeError('bad type')])``.
The difference between them is that ``ExceptionGroup`` can only wrap
``Exception`` subclasses while ``BaseExceptionGroup`` can wrap any
``BaseException`` subclass. A factory method that inspects the nested
execptions and selects between ``ExceptionGroup`` and ``BaseExceptionGroup``
makes the choice automatic. In the rest of the document, unless stated
otherwise, when we refer to ``ExceptionGroup`` we mean either an
``ExceptionGroup`` or a ``BaseExceptionGroup``.
Copy link
Member

Choose a reason for hiding this comment

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

This last thing is slightly awkward. Maybe we should write "exception group" when either can be used and "ExceptionGroup" when that specific type is meant? (Maybe do this in a separate PR if it affects a lot of locations in the doc, as I anticipate.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, let's fix and merge the two open PRs and then I'll change this in a new one.


The ``ExceptionGroup`` class exposes these parameters in the fields ``message``
and ``errors``. A nested exception can also be an ``ExceptionGroup`` so the
Expand Down Expand Up @@ -944,17 +954,20 @@ propose in this PEP will not break any existing code:
Programs will only be impacted by the changes proposed in this PEP once they
begin to use ``ExceptionGroups`` and ``except*``.

* An important concern was that ``except Exception:`` will continue to catch
almost all exceptions, and by making ``ExceptionGroup`` extend ``Exception``
we ensured that this will be the case. ``BaseExceptionGroups`` will not be
caught, which is appropriate because they include exceptions that would not
have been caught by ``except Exception``.

Once programs begin to use these features, there will be migration issues to
consider:

* An ``except Exception:`` clause will not catch ``ExceptionGroup`` because it
is derived from ``BaseException``. Any such clause will need to be replaced
by ``except (Exception, ExceptionGroup):`` or ``except *Exception:``.

* Similarly, any ``except T:`` clause that wraps code which is now potentially
raising ``ExceptionGroup`` needs to become ``except *T:``, and its body may
need to be updated.
* An ``except T:`` clause that wraps code which is now potentially raising
``ExceptionGroup`` may need to become ``except *T:``, and its body may
need to be updated. This means that raising an ``ExceptionGroup`` is an
API-breaking change and will likely be done in new APIs rather than
added to existing ones.

* Libraries that need to support older Python versions will not be able to use
``except*`` or raise ``ExceptionGroups``.
Expand Down