Skip to content

Commit 550b796

Browse files
authored
Added BaseExceptionGroup.__init__, following CPython (#142)
1 parent 2a84dfd commit 550b796

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
1111
types to define defaults for their generic arguments (defaulting to
1212
``BaseExceptionGroup[BaseException]`` and ``ExceptionGroup[Exception]``)
1313
(PR by @mikenerone)
14+
- Changed ``BaseExceptionGroup.__init__()`` to directly call
15+
``BaseException.__init__()`` instead of the superclass ``__init__()`` in order to
16+
emulate the CPython behavior (broken or not) (PR by @cfbolz)
1417

1518
**1.2.2**
1619

src/exceptiongroup/_exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ def __new__(
105105
instance._exceptions = __exceptions
106106
return instance
107107

108+
def __init__(
109+
self,
110+
__message: str,
111+
__exceptions: Sequence[_BaseExceptionT_co],
112+
*args: object,
113+
) -> None:
114+
BaseException.__init__(self, __message, __exceptions, *args)
115+
108116
def add_note(self, note: str) -> None:
109117
if not isinstance(note, str):
110118
raise TypeError(

tests/test_exceptions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,23 @@ def test_repr():
844844

845845
group = ExceptionGroup("foo", [ValueError(1), RuntimeError("bar")])
846846
assert repr(group) == "ExceptionGroup('foo', [ValueError(1), RuntimeError('bar')])"
847+
848+
849+
def test_bug_exceptiongroup_has_no_init():
850+
assert (
851+
BaseExceptionGroup.__init__
852+
is ExceptionGroup.__init__
853+
is not BaseException.__init__
854+
)
855+
for base in [BaseExceptionGroup, ExceptionGroup]:
856+
857+
class MyException(Exception):
858+
def __init__(self, message):
859+
pytest.fail("should not be reached")
860+
861+
class MyExceptionGroup(base, MyException):
862+
pass
863+
864+
MyExceptionGroup(
865+
"...", [Exception()]
866+
) # does not try to call MyException.__init__

0 commit comments

Comments
 (0)