Skip to content

Commit 8158778

Browse files
committed
gh-99572: clarify doc and add tests
1 parent b0e1f9c commit 8158778

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Doc/library/exceptions.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,10 @@ their subgroups based on the types of the contained exceptions.
965965
def derive(self, excs):
966966
return Errors(excs, self.exit_code)
967967

968-
Like :exc:`ExceptionGroup`, any subclass of :exc:`BaseExceptionGroup` which
969-
is also a subclass of :exc:`Exception` can only wrap instances of
970-
:exc:`Exception`.
968+
Subclasses of :exc:`BaseExceptionGroup` cannot wrap :exc:`BaseException`s
969+
if they extend :exc:`Exception`, as for :exc:`ExceptionGroup`. However,
970+
there is no further restriction: a :exc:`BaseExceptionGroup` subclass
971+
which extends :exc:`KeyError` could wrap any :exc:`Exception`.
971972

972973
.. versionadded:: 3.11
973974

Lib/test/test_exception_group.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ class MyEG(BaseExceptionGroup, ValueError):
102102
with self.assertRaisesRegex(TypeError, msg):
103103
MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])
104104

105+
def test_EG_and_specific_subclass_can_wrap_any_nonbase_exception(self):
106+
class MyEG(ExceptionGroup, ValueError):
107+
pass
108+
109+
# The restriction is specific to Exception, not "the other base class"
110+
MyEG("eg", [ValueError(12), Exception()])
111+
112+
def test_BEG_and_specific_subclass_can_wrap_any_nonbase_exception(self):
113+
class MyEG(BaseExceptionGroup, ValueError):
114+
pass
115+
116+
# The restriction is specific to Exception, not "the other base class"
117+
MyEG("eg", [ValueError(12), Exception()])
118+
105119

106120
def test_BEG_subclass_wraps_anything(self):
107121
class MyBEG(BaseExceptionGroup):

0 commit comments

Comments
 (0)