diff --git a/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py b/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py index b1bf701c4b8c..8d8ea0cbebed 100644 --- a/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py +++ b/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py @@ -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 @@ -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() # ----------- @@ -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() diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 0a6dc57b05b8..cfb6353cbe00 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -2076,8 +2076,13 @@ if sys.version_info >= (3, 11): # 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: ... - def __init__(self, message: str, exceptions: Sequence[_BaseExceptionT_co], /) -> None: ... @property def message(self) -> str: ... @property