Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
sobolevn committed Nov 21, 2022
commit 9c06c0a558a3c9ec45e225fcef875f468d99005a
23 changes: 10 additions & 13 deletions test_cases/stdlib/builtins/check_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# -----------

assert_type(beg.subgroup(KeyboardInterrupt), BaseExceptionGroup[KeyboardInterrupt] | None)
assert_type(beg.subgroup((KeyboardInterrupt, SystemExit)), BaseExceptionGroup[BaseException] | None)
assert_type(beg.subgroup((KeyboardInterrupt,)), BaseExceptionGroup[KeyboardInterrupt] | None)

def is_base_exc(exc: BaseException) -> bool:
return isinstance(exc, BaseException)
Expand All @@ -47,10 +47,9 @@ def unrelated_subgroup(exc: KeyboardInterrupt) -> bool:
beg.subgroup(is_system_exit) # type: ignore
beg.subgroup(unrelated_subgroup) # type: ignore

# This might be strange at first, `ValueError` is not listed in the generic params.
# But, this seems practical. Some exception types can be down-casted.
# `Exception`` subgroup returns `ExceptionGroup`:
assert_type(beg.subgroup(ValueError), ExceptionGroup[ValueError] | None)
assert_type(beg.subgroup((ValueError, KeyError)), ExceptionGroup[Exception] | None)
assert_type(beg.subgroup((ValueError,)), ExceptionGroup[ValueError] | None)

# Callable are harder, we don't support cast to `ExceptionGroup` here.
# Because callables might return `True` the first time. And `BaseExceptionGroup`
Expand All @@ -59,11 +58,11 @@ def unrelated_subgroup(exc: KeyboardInterrupt) -> bool:
def is_exception(exc: Exception) -> bool:
return isinstance(exc, Exception)

def is_exception_or_beg(exc: Exception | BaseExceptionGroup[SystemError]) -> bool:
def is_exception_or_beg(exc: Exception | BaseExceptionGroup[SystemExit]) -> bool:
return isinstance(exc, Exception)

# This is an error because of the `Exception` argument type,
# while `SystemError` is needed.
# while `SystemExit` is needed instead.
beg.subgroup(is_exception_or_beg) # type: ignore

# This is an error, because `BaseExceptionGroup` is not an `Exception`
Expand All @@ -77,8 +76,8 @@ def is_exception_or_beg(exc: Exception | BaseExceptionGroup[SystemError]) -> boo
beg.split(KeyboardInterrupt), tuple[BaseExceptionGroup[KeyboardInterrupt] | None, BaseExceptionGroup[SystemExit] | None]
)
assert_type(
beg.split((KeyboardInterrupt, SystemExit)),
tuple[BaseExceptionGroup[BaseException] | None, BaseExceptionGroup[SystemExit] | None],
beg.split((KeyboardInterrupt,)),
tuple[BaseExceptionGroup[KeyboardInterrupt] | None, BaseExceptionGroup[SystemExit] | None],
)
assert_type(
beg.split(ValueError), # there are no `ValueError` items in there, but anyway
Expand Down Expand Up @@ -157,7 +156,7 @@ def split_callable1(exc: ValueError | KeyError | SystemExit | BaseExceptionGroup

assert_type(eg.subgroup(Exception), ExceptionGroup[Exception] | None)
assert_type(eg.subgroup(ValueError), ExceptionGroup[ValueError] | None)
assert_type(eg.subgroup((ValueError, KeyError)), ExceptionGroup[Exception] | None)
assert_type(eg.subgroup((ValueError,)), ExceptionGroup[ValueError] | None)

def subgroup_eg1(exc: ValueError | KeyError | ExceptionGroup[ValueError | KeyError]) -> bool:
return True
Expand All @@ -177,9 +176,7 @@ def subgroup_eg2(exc: ValueError | KeyError) -> bool:
# --------

assert_type(eg.split(TypeError), tuple[ExceptionGroup[TypeError] | None, ExceptionGroup[ValueError | KeyError] | None])
assert_type(
eg.split((TypeError, IndexError)), tuple[ExceptionGroup[Exception] | None, ExceptionGroup[ValueError | KeyError] | None]
)
assert_type(eg.split((TypeError,)), tuple[ExceptionGroup[TypeError] | None, ExceptionGroup[ValueError | KeyError] | None])
assert_type(
eg.split(is_exception), tuple[ExceptionGroup[ValueError | KeyError] | None, ExceptionGroup[ValueError | KeyError] | None]
)
Expand Down Expand Up @@ -226,7 +223,7 @@ class CustomBaseGroup(BaseExceptionGroup[_BE]):
# -----------

assert_type(cb1.subgroup(KeyboardInterrupt), BaseExceptionGroup[KeyboardInterrupt] | None)
assert_type(cb2.subgroup((KeyboardInterrupt, SystemExit)), BaseExceptionGroup[BaseException] | None)
assert_type(cb2.subgroup((KeyboardInterrupt,)), BaseExceptionGroup[KeyboardInterrupt] | None)

assert_type(cb1.subgroup(ValueError), ExceptionGroup[ValueError] | None)
assert_type(cb2.subgroup((KeyError, TypeError)), ExceptionGroup[Exception] | None)
Expand Down