Add defaults to the generic parameters of BaseExceptionGroup and ExceptionGroup (#12258)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
This commit is contained in:
Kanishk Pachauri
2024-07-11 18:41:19 +05:30
committed by GitHub
parent 5344c4b7ae
commit b24dfccb74
2 changed files with 33 additions and 2 deletions

View File

@@ -321,3 +321,34 @@ if sys.version_info >= (3, 11):
# Note, that `Self` type is not preserved in runtime.
assert_type(cg1.derive([ValueError()]), ExceptionGroup[ValueError])
assert_type(cg1.derive([KeyboardInterrupt()]), BaseExceptionGroup[KeyboardInterrupt])
# Additional tests
# ==============================
def test_exception_group_default_type() -> None:
try:
ex: ExceptionGroup = ExceptionGroup("", [ValueError("a"), ValueError("b")])
raise ex
except ExceptionGroup as e:
assert all(isinstance(exc, ValueError) for exc in e.exceptions)
def test_exception_group_with_specific_type() -> None:
try:
ex: ExceptionGroup[ValueError] = ExceptionGroup("", [ValueError("a"), ValueError("b")])
raise ex
except ExceptionGroup as e:
assert all(isinstance(exc, ValueError) for exc in e.exceptions)
def test_base_exception_group_default_type() -> None:
try:
ex: BaseExceptionGroup = BaseExceptionGroup("", [SystemExit("a"), SystemExit("b")])
raise ex
except BaseExceptionGroup as e:
assert all(isinstance(exc, SystemExit) for exc in e.exceptions)
def test_base_exception_group_with_specific_type() -> None:
try:
ex: BaseExceptionGroup[SystemExit] = BaseExceptionGroup("", [SystemExit("a"), SystemExit("b")])
raise ex
except BaseExceptionGroup as e:
assert all(isinstance(exc, SystemExit) for exc in e.exceptions)