mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-08 21:14:48 +08:00
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:
@@ -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)
|
||||
|
||||
@@ -2006,9 +2006,9 @@ if sys.version_info >= (3, 10):
|
||||
class EncodingWarning(Warning): ...
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
_BaseExceptionT_co = TypeVar("_BaseExceptionT_co", bound=BaseException, covariant=True)
|
||||
_BaseExceptionT_co = TypeVar("_BaseExceptionT_co", bound=BaseException, covariant=True, default=BaseException)
|
||||
_BaseExceptionT = TypeVar("_BaseExceptionT", bound=BaseException)
|
||||
_ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True)
|
||||
_ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True, default=Exception)
|
||||
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)
|
||||
|
||||
# See `check_exception_group.py` for use-cases and comments.
|
||||
|
||||
Reference in New Issue
Block a user