From d1cd0f2583e63e6c5fc56de59cd05a993446ab52 Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 27 Feb 2025 17:49:40 +0100
Subject: [PATCH 1/5] change to prioritizing baseexceptiongroup->exceptiongroup
if non-base exceptions, vs making custom baseexceptiongroups work
---
.../builtins/check_exception_group-py311.py | 12 +++++++++---
stdlib/builtins.pyi | 4 +++-
2 files changed, 12 insertions(+), 4 deletions(-)
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..94b091e0a08a 100644
--- a/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py
+++ b/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py
@@ -1,7 +1,7 @@
from __future__ import annotations
import sys
-from typing import TypeVar
+from typing import Never, TypeVar
from typing_extensions import assert_type
if sys.version_info >= (3, 11):
@@ -23,7 +23,7 @@
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 +218,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..879da2129061 100644
--- a/stdlib/builtins.pyi
+++ b/stdlib/builtins.pyi
@@ -2076,8 +2076,10 @@ if sys.version_info >= (3, 11):
# See `check_exception_group.py` for use-cases and comments.
class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
+ @overload
+ def __new__(cls, 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
From 6b780e242ade68c17a399398328fab58065e874b Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 27 Feb 2025 17:54:40 +0100
Subject: [PATCH 2/5] fix Never import
---
.../test_cases/builtins/check_exception_group-py311.py | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
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 94b091e0a08a..72eb15c8b630 100644
--- a/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py
+++ b/stdlib/@tests/test_cases/builtins/check_exception_group-py311.py
@@ -1,8 +1,8 @@
from __future__ import annotations
import sys
-from typing import Never, TypeVar
-from typing_extensions import assert_type
+from typing import TypeVar
+from typing_extensions import assert_type, Never
if sys.version_info >= (3, 11):
# This can be removed later, but right now Flake8 does not know
@@ -21,8 +21,6 @@
# `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, ExceptionGroup[ValueError])
# .subgroup()
From 38779ddf0ac0b995aa3a40d724f23ee36b7d5b3f Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 27 Feb 2025 17:55:13 +0100
Subject: [PATCH 3/5] fix Never import
---
.../@tests/test_cases/builtins/check_exception_group-py311.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 72eb15c8b630..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, Never
+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
From 8ef47dc74538cb37bc59b9949a8f6a5c9e6d5e4c Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 27 Feb 2025 18:00:40 +0100
Subject: [PATCH 4/5] specify cls
---
stdlib/builtins.pyi | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi
index 879da2129061..5770f19b3097 100644
--- a/stdlib/builtins.pyi
+++ b/stdlib/builtins.pyi
@@ -2077,7 +2077,9 @@ if sys.version_info >= (3, 11):
# See `check_exception_group.py` for use-cases and comments.
class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
- def __new__(cls, message: str, exceptions: Sequence[_ExceptionT_co], /) -> ExceptionGroup[_ExceptionT_co]: ...
+ def __new__(
+ cls: ExceptionGroup[_ExceptionT_co], message: str, exceptions: Sequence[_ExceptionT_co], /
+ ) -> ExceptionGroup[_ExceptionT_co]: ...
@overload
def __new__(cls, message: str, exceptions: Sequence[_BaseExceptionT_co], /) -> Self: ...
@property
From 2f44dfaf0de6a2194c2e434a7e74745f7ddadd3d Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 27 Feb 2025 18:04:44 +0100
Subject: [PATCH 5/5] I should maybe figure out how to run all the proper tests
locally
---
stdlib/builtins.pyi | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi
index 5770f19b3097..cfb6353cbe00 100644
--- a/stdlib/builtins.pyi
+++ b/stdlib/builtins.pyi
@@ -2077,7 +2077,8 @@ if sys.version_info >= (3, 11):
# See `check_exception_group.py` for use-cases and comments.
class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
- def __new__(
+ # 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