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

BaseExceptionGroup initialized with non-base exceptions now return ExceptionGroup #13554

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions stdlib/@tests/test_cases/builtins/check_exception_group-py311.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys
from typing import TypeVar
from typing_extensions import assert_type
from typing_extensions import Never, assert_type

if sys.version_info >= (3, 11):
# This can be removed later, but right now Flake8 does not know
Expand All @@ -21,9 +21,7 @@

# `BaseExceptionGroup` can work with `Exception`:
beg2 = BaseExceptionGroup("x", [ValueError()])
# FIXME: this is not right, runtime returns `ExceptionGroup` instance instead,
# but I am unable to represent this with types right now.
assert_type(beg2, BaseExceptionGroup[ValueError])
assert_type(beg2, ExceptionGroup[ValueError])

# .subgroup()
# -----------
Expand Down Expand Up @@ -218,7 +216,13 @@ class CustomBaseGroup(BaseExceptionGroup[_BE]): ...

cb1 = CustomBaseGroup("x", [SystemExit()])
assert_type(cb1, CustomBaseGroup[SystemExit])
cb2 = CustomBaseGroup("x", [ValueError()])

# Custom subclasses that don't implement __new__ are now kinda borked when
# passing non-base exceptions.
# With current typing it's not possible to have this working at the same time
# as making BaseExceptionGroup initialized with non-base exceptions return ExceptionGroup
assert_type(CustomBaseGroup("x", [ValueError()]), CustomBaseGroup[Never])
cb2: CustomBaseGroup[ValueError] = CustomBaseGroup("x", [ValueError()])
assert_type(cb2, CustomBaseGroup[ValueError])

# .subgroup()
Expand Down
7 changes: 6 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2076,8 +2076,13 @@

# See `check_exception_group.py` for use-cases and comments.
class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
# mypy thinks this is an invalid type for cls/self
def __new__( # type: ignore[misc]
cls: ExceptionGroup[_ExceptionT_co], message: str, exceptions: Sequence[_ExceptionT_co], /
) -> ExceptionGroup[_ExceptionT_co]: ...
@overload
def __new__(cls, message: str, exceptions: Sequence[_BaseExceptionT_co], /) -> Self: ...

Check failure on line 2085 in stdlib/builtins.pyi

View workflow job for this annotation

GitHub Actions / Test typeshed with pyright (Linux, 3.12)

Mismatch between signature of __new__ and __init__ in class "BaseExceptionGroup[_BaseExceptionT_co@BaseExceptionGroup]"   Signature of __init__ is "(*args: object) -> None"   Signature of __new__ is "(message: str, exceptions: Sequence[_BaseExceptionT_co@BaseExceptionGroup], /) -> BaseExceptionGroup[_BaseExceptionT_co@BaseExceptionGroup]" (reportInconsistentConstructor)
def __init__(self, message: str, exceptions: Sequence[_BaseExceptionT_co], /) -> None: ...
@property
def message(self) -> str: ...
@property
Expand Down
Loading