8000 gh-99619: fix error in documentation of ExceptionGroup.derive() (GH-9… · python/cpython@5d9183c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d9183c

Browse files
authored
gh-99619: fix error in documentation of ExceptionGroup.derive() (GH-99621)
1 parent 8f024a0 commit 5d9183c

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

Doc/library/exceptions.rst

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,21 +934,42 @@ their subgroups based on the types of the contained exceptions.
934934

935935
.. method:: derive(excs)
936936

937-
Returns an exception group with the same :attr:`message`,
938-
:attr:`__traceback__`, :attr:`__cause__`, :attr:`__context__`
939-
and :attr:`__notes__` but which wraps the exceptions in ``excs``.
937+
Returns an exception group with the same :attr:`message`, but which
938+
wraps the exceptions in ``excs``.
940939

941940
This method is used by :meth:`subgroup` and :meth:`split`. A
942941
subclass needs to override it in order to make :meth:`subgroup`
943942
and :meth:`split` return instances of the subclass rather
944-
than :exc:`ExceptionGroup`. ::
943+
than :exc:`ExceptionGroup`.
944+
945+
:meth:`subgroup` and :meth:`split` copy the :attr:`__traceback__`,
946+
:attr:`__cause__`, :attr:`__context__` and :attr:`__notes__` fields from
947+
the original exception group to the one returned by :meth:`derive`, so
948+
these fields do not need to be updated by :meth:`derive`. ::
945949

946950
>>> class MyGroup(ExceptionGroup):
947951
... def derive(self, exc):
948952
... return MyGroup(self.message, exc)
949953
...
950-
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
951-
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
954+
>>> e = MyGroup("eg", [ValueError(1), TypeError(2)])
955+
>>> e.add_note("a note")
956+
>>> e.__context__ = Exception("context")
957+
>>> e.__cause__ = Exception("cause")
958+
>>> try:
959+
... raise e
960+
... except Exception as e:
961+
... exc = e
962+
...
963+
>>> match, rest = exc.split(ValueError)
964+
>>> exc, exc.__context__, exc.__cause__, exc.__notes__
965+
(MyGroup('eg', [ValueError(1), TypeError(2)]), Exception('context'), Exception('cause'), ['a note'])
966+
>>> match, match.__context__, match.__cause__, match.__notes__
967+
(MyGroup('eg', [ValueError(1)]), Exception('context'), Exception('cause'), ['a note'])
968+
>>> rest, rest.__context__, rest.__cause__, rest.__notes__
969+
(MyGroup('eg', [TypeError(2)]), Exception('context'), Exception('cause'), ['a note'])
970+
>>> exc.__traceback__ is match.__traceback__ is rest.__traceback__
971+
True
972+
952973

953974
Note that :exc:`BaseExceptionGroup` defines :meth:`__new__`, so
954975
subclasses that need a different constructor signature need to

0 commit comments

Comments
 (0)
0