Skip to content

Commit e0654bd

Browse files
ExceptionGroup: make it generic (#7626)
1 parent 7f9be7f commit e0654bd

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

stdlib/builtins.pyi

+36-11
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ from typing import (
5252
SupportsInt,
5353
SupportsRound,
5454
TypeVar,
55-
Union,
5655
overload,
5756
)
5857
from typing_extensions import Literal, SupportsIndex, TypeGuard, final
@@ -1747,19 +1746,45 @@ if sys.version_info >= (3, 10):
17471746
class EncodingWarning(Warning): ...
17481747

17491748
if sys.version_info >= (3, 11):
1750-
_SplitCondition = Union[type[BaseException], tuple[type[BaseException], ...], Callable[[BaseException], bool]]
1749+
_BaseExceptionT_co = TypeVar("_BaseExceptionT_co", bound=BaseException, covariant=True)
1750+
_BaseExceptionT = TypeVar("_BaseExceptionT", bound=BaseException)
1751+
_ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True)
1752+
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)
17511753

1752-
class BaseExceptionGroup(BaseException):
1753-
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[BaseException]) -> Self: ...
1754+
class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
1755+
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_BaseExceptionT_co]) -> Self: ...
17541756
@property
17551757
def message(self) -> str: ...
17561758
@property
1757-
def exceptions(self) -> tuple[BaseException, ...]: ...
1758-
def subgroup(self: Self, __condition: _SplitCondition) -> Self | None: ...
1759-
def split(self: Self, __condition: _SplitCondition) -> tuple[Self | None, Self | None]: ...
1760-
def derive(self: Self, __excs: Sequence[BaseException]) -> Self: ...
1759+
def exceptions(self) -> tuple[_BaseExceptionT_co | BaseExceptionGroup[_BaseExceptionT_co], ...]: ...
1760+
@overload
1761+
def subgroup(
1762+
self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...]
1763+
) -> BaseExceptionGroup[_BaseExceptionT] | None: ...
1764+
@overload
1765+
def subgroup(self: Self, __condition: Callable[[_BaseExceptionT_co], bool]) -> Self | None: ...
1766+
@overload
1767+
def split(
1768+
self: Self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...]
1769+
) -> tuple[BaseExceptionGroup[_BaseExceptionT] | None, Self | None]: ...
1770+
@overload
1771+
def split(self: Self, __condition: Callable[[_BaseExceptionT_co], bool]) -> tuple[Self | None, Self | None]: ...
1772+
def derive(self: Self, __excs: Sequence[_BaseExceptionT_co]) -> Self: ...
17611773

1762-
class ExceptionGroup(BaseExceptionGroup, Exception):
1763-
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[Exception]) -> Self: ...
1774+
class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception):
1775+
def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_ExceptionT_co]) -> Self: ...
17641776
@property
1765-
def exceptions(self) -> tuple[Exception, ...]: ...
1777+
def exceptions(self) -> tuple[_ExceptionT_co | ExceptionGroup[_ExceptionT_co], ...]: ...
1778+
# We accept a narrower type, but that's OK.
1779+
@overload # type: ignore[override]
1780+
def subgroup(
1781+
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
1782+
) -> ExceptionGroup[_ExceptionT] | None: ...
1783+
@overload
1784+
def subgroup(self: Self, __condition: Callable[[_ExceptionT_co], bool]) -> Self | None: ...
1785+
@overload # type: ignore[override]
1786+
def split(
1787+
self: Self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
1788+
) -> tuple[ExceptionGroup[_ExceptionT] | None, Self | None]: ...
1789+
@overload
1790+
def split(self: Self, __condition: Callable[[_ExceptionT_co], bool]) -> tuple[Self | None, Self | None]: ...

0 commit comments

Comments
 (0)