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

Use __new__() only for initialization #52

Merged
merged 4 commits into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -7,6 +7,7 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

- Backported upstream fix for gh-99553 (custom subclasses of ``BaseExceptionGroup`` that
also inherit from ``Exception`` should not be able to wrap base exceptions)
- Moved all initialization code to ``__new__()`` (thus matching Python 3.11 behavior)

**1.0.4**

Expand Down
14 changes: 5 additions & 9 deletions src/exceptiongroup/_exceptions.py
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Callable, Sequence
from functools import partial
from inspect import getmro, isclass
from typing import TYPE_CHECKING, Any, Generic, Type, TypeVar, cast, overload
from typing import TYPE_CHECKING, Generic, Type, TypeVar, cast, overload

if TYPE_CHECKING:
from typing import Self
Expand Down Expand Up @@ -79,14 +79,10 @@ def __new__(
f"Cannot nest BaseExceptions in {cls.__name__!r}"
)

return super().__new__(cls, __message, __exceptions)

def __init__(
self, __message: str, __exceptions: Sequence[_BaseExceptionT_co], *args: Any
):
super().__init__(__message, __exceptions, *args)
self._message = __message
self._exceptions = __exceptions
instance = super().__new__(cls, __message, __exceptions)
instance._message = __message
instance._exceptions = __exceptions
return instance

def add_note(self, note: str) -> None:
if not isinstance(note, str):
Expand Down