From cd608d4256cf00b52e4aeaba07b0eaead4193dbb Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 14 Apr 2022 18:41:54 -0700 Subject: [PATCH 1/3] ExceptionGroup: make it generic See motivation in #91549. --- stdlib/builtins.pyi | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index ce2042bcac63..65ab09699d56 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1747,19 +1747,44 @@ if sys.version_info >= (3, 10): class EncodingWarning(Warning): ... if sys.version_info >= (3, 11): - _SplitCondition = Union[type[BaseException], tuple[type[BaseException], ...], Callable[[BaseException], bool]] + _BaseExceptionT_co = TypeVar("_BaseExceptionT_co", bound=BaseException, covariant=True) + _BaseExceptionT = TypeVar("_BaseExceptionT", bound=BaseException) + _ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True) + _ExceptionT = TypeVar("_ExceptionT", bound=Exception) - class BaseExceptionGroup(BaseException): - def __new__(cls: type[Self], __message: str, __exceptions: Sequence[BaseException]) -> Self: ... + class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]): + def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_BaseExceptionT_co]) -> Self: ... @property def message(self) -> str: ... @property - def exceptions(self) -> tuple[BaseException, ...]: ... - def subgroup(self: Self, __condition: _SplitCondition) -> Self | None: ... - def split(self: Self, __condition: _SplitCondition) -> tuple[Self | None, Self | None]: ... - def derive(self: Self, __excs: Sequence[BaseException]) -> Self: ... + def exceptions(self) -> tuple[_BaseExceptionT_co | BaseExceptionGroup[_BaseExceptionT_co], ...]: ... + @overload + def subgroup( + self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...] + ) -> BaseExceptionGroup[_BaseExceptionT] | None: ... + @overload + def subgroup(self: Self, __condition: Callable[[_BaseExceptionT_co], bool]) -> Self | None: ... + @overload + def split( + self: Self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...] + ) -> tuple[BaseExceptionGroup[_BaseExceptionT] | None, Self | None]: ... + @overload + def split(self: Self, __condition: Callable[[_BaseExceptionT_co], bool]) -> tuple[Self | None, Self | None]: ... + def derive(self: Self, __excs: Sequence[_BaseExceptionT_co]) -> Self: ... - class ExceptionGroup(BaseExceptionGroup, Exception): - def __new__(cls: type[Self], __message: str, __exceptions: Sequence[Exception]) -> Self: ... + class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception): + def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_ExceptionT_co]) -> Self: ... @property - def exceptions(self) -> tuple[Exception, ...]: ... + def exceptions(self) -> tuple[_ExceptionT_co | ExceptionGroup[_ExceptionT_co], ...]: ... + @overload + def subgroup( + self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...] + ) -> ExceptionGroup[_ExceptionT] | None: ... + @overload + def subgroup(self: Self, __condition: Callable[[_ExceptionT_co], bool]) -> Self | None: ... + @overload + def split( + self: Self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...] + ) -> tuple[ExceptionGroup[_ExceptionT] | None, Self | None]: ... + @overload + def split(self: Self, __condition: Callable[[_ExceptionT_co], bool]) -> tuple[Self | None, Self | None]: ... From f07aec03162210ba66cb9806b2a594d7c3f5127b Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 14 Apr 2022 18:50:53 -0700 Subject: [PATCH 2/3] type ignore --- stdlib/builtins.pyi | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 65ab09699d56..872bccd0db8f 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1776,13 +1776,14 @@ if sys.version_info >= (3, 11): def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_ExceptionT_co]) -> Self: ... @property def exceptions(self) -> tuple[_ExceptionT_co | ExceptionGroup[_ExceptionT_co], ...]: ... - @overload + # We accept a narrower type, but that's OK. + @overload # type: ignore[override] def subgroup( self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...] ) -> ExceptionGroup[_ExceptionT] | None: ... @overload def subgroup(self: Self, __condition: Callable[[_ExceptionT_co], bool]) -> Self | None: ... - @overload + @overload # type: ignore[override] def split( self: Self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...] ) -> tuple[ExceptionGroup[_ExceptionT] | None, Self | None]: ... From 4ce70e3a27fa178ad722e0d9605293628a5d201c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 14 Apr 2022 18:52:41 -0700 Subject: [PATCH 3/3] who needs Union --- stdlib/builtins.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 872bccd0db8f..9c5dfcfef22f 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -52,7 +52,6 @@ from typing import ( SupportsInt, SupportsRound, TypeVar, - Union, overload, ) from typing_extensions import Literal, SupportsIndex, TypeGuard, final