@@ -934,21 +934,42 @@ their subgroups based on the types of the contained exceptions.
934
934
935
935
.. method :: derive(excs)
936
936
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 ``.
940
939
941
940
This method is used by :meth: `subgroup ` and :meth: `split `. A
942
941
subclass needs to override it in order to make :meth: `subgroup `
943
942
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 `. ::
945
949
946
950
>>> class MyGroup(ExceptionGroup):
947
951
... def derive(self, exc):
948
952
... return MyGroup(self.message, exc)
949
953
...
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
+
952
973
953
974
Note that :exc: `BaseExceptionGroup ` defines :meth: `__new__ `, so
954
975
subclasses that need a different constructor signature need to
0 commit comments