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
[pre-commit.ci] auto fixes from pre-commit.com hooks
  • Loading branch information
pre-commit-ci[bot] committed Nov 20, 2022
commit 5daa26c7d58ff8109825de8e9ab4abee92a31c74
49 changes: 3 additions & 46 deletions test_cases/stdlib/builtins/check_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,26 @@
# but I am unable to represent this with types right now.
assert_type(beg2, BaseExceptionGroup[ValueError])


# .subgroup()
# -----------

assert_type(beg.subgroup(KeyboardInterrupt), BaseExceptionGroup[KeyboardInterrupt] | None)
assert_type(beg.subgroup((KeyboardInterrupt, SystemExit)), BaseExceptionGroup[BaseException] | None)
Comment thread
sobolevn marked this conversation as resolved.
Outdated


def is_base_exc(exc: BaseException) -> bool:
return isinstance(exc, BaseException)


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


# This one does not have `BaseExceptionGroup` part,
# this is why we treat as an error.
def is_system_exit(exc: SystemExit) -> bool:
return isinstance(exc, SystemExit)


def unrelated_subgroup(exc: KeyboardInterrupt) -> bool:
return False


assert_type(beg.subgroup(is_base_exc), BaseExceptionGroup[SystemExit] | None)
assert_type(beg.subgroup(is_specific), BaseExceptionGroup[SystemExit] | None)
beg.subgroup(is_system_exit) # type: ignore
Expand All @@ -62,15 +56,12 @@ def unrelated_subgroup(exc: KeyboardInterrupt) -> bool:
# Because callables might return `True` the first time. And `BaseExceptionGroup`
# will stick, no matter what arguments are.


def is_exception(exc: Exception) -> bool:
return isinstance(exc, Exception)


def is_exception_or_beg(exc: Exception | BaseExceptionGroup[SystemError]) -> bool:
Comment thread
sobolevn marked this conversation as resolved.
Outdated
return isinstance(exc, Exception)


# This is an error because of the `Exception` argument type,
# while `SystemError` is needed.
beg.subgroup(is_exception_or_beg) # type: ignore
Expand All @@ -79,7 +70,6 @@ def is_exception_or_beg(exc: Exception | BaseExceptionGroup[SystemError]) -> boo
# subclass. It is required.
beg.subgroup(is_exception) # type: ignore


# .split()
# --------

Expand Down Expand Up @@ -109,11 +99,9 @@ def is_exception_or_beg(exc: Exception | BaseExceptionGroup[SystemError]) -> boo
tuple[ExceptionGroup[ValueError] | None, BaseExceptionGroup[ValueError | KeyError | SystemExit] | None,],
)


def split_callable1(exc: ValueError | KeyError | SystemExit | BaseExceptionGroup[ValueError | KeyError | SystemExit]) -> bool:
return True


assert_type(
to_split.split(split_callable1), # Concrete type is ok
tuple[
Expand All @@ -131,14 +119,12 @@ def split_callable1(exc: ValueError | KeyError | SystemExit | BaseExceptionGroup
# `Exception` cannot be used: `BaseExceptionGroup` is not a subtype of it.
to_split.split(is_exception) # type: ignore


# .derive()
# ---------

assert_type(beg.derive([ValueError()]), ExceptionGroup[ValueError])
assert_type(beg.derive([KeyboardInterrupt()]), BaseExceptionGroup[KeyboardInterrupt])


# ExceptionGroup
# ==============

Expand All @@ -154,7 +140,6 @@ def split_callable1(exc: ValueError | KeyError | SystemExit | BaseExceptionGroup
# `ExceptionGroup` cannot work with `BaseException`:
ExceptionGroup("x", [SystemExit()]) # type: ignore


# .subgroup()
# -----------

Expand All @@ -174,15 +159,12 @@ def split_callable1(exc: ValueError | KeyError | SystemExit | BaseExceptionGroup
assert_type(eg.subgroup(ValueError), ExceptionGroup[ValueError] | None)
assert_type(eg.subgroup((ValueError, KeyError)), ExceptionGroup[Exception] | None)
Comment thread
sobolevn marked this conversation as resolved.
Outdated


def subgroup_eg1(exc: ValueError | KeyError | ExceptionGroup[ValueError | KeyError]) -> bool:
return True


def subgroup_eg2(exc: ValueError | KeyError) -> bool:
return True


assert_type(eg.subgroup(subgroup_eg1), ExceptionGroup[ValueError | KeyError] | None)
assert_type(eg.subgroup(is_exception), ExceptionGroup[ValueError | KeyError] | None)
assert_type(eg.subgroup(is_base_exc), ExceptionGroup[ValueError | KeyError] | None)
Expand All @@ -191,7 +173,6 @@ def subgroup_eg2(exc: ValueError | KeyError) -> bool:
# Does not have `ExceptionGroup` part:
eg.subgroup(subgroup_eg2) # type: ignore


# .split()
# --------

Expand All @@ -213,21 +194,18 @@ def subgroup_eg2(exc: ValueError | KeyError) -> bool:
def value_or_key_error(exc: ValueError | KeyError) -> bool:
return isinstance(exc, (ValueError, KeyError))


eg.split(value_or_key_error) # type: ignore

# `ExceptionGroup` cannot have direct `BaseException` subclasses inside.
eg.split(BaseException) # type: ignore
eg.split((SystemExit, GeneratorExit)) # type: ignore


# .derive()
# ---------

assert_type(eg.derive([ValueError()]), ExceptionGroup[ValueError])
assert_type(eg.derive([KeyboardInterrupt()]), BaseExceptionGroup[KeyboardInterrupt])


# BaseExceptionGroup Custom Subclass
# ==================================
# In some cases `Self` type can be preserved in runtime,
Expand All @@ -236,17 +214,14 @@ def value_or_key_error(exc: ValueError | KeyError) -> bool:

_BE = TypeVar("_BE", bound=BaseException)


class CustomBaseGroup(BaseExceptionGroup[_BE]):
...


cb1 = CustomBaseGroup("x", [SystemExit()])
assert_type(cb1, CustomBaseGroup[SystemExit])
cb2 = CustomBaseGroup("x", [ValueError()])
assert_type(cb2, CustomBaseGroup[ValueError])


# .subgroup()
# -----------

Expand All @@ -256,45 +231,39 @@ class CustomBaseGroup(BaseExceptionGroup[_BE]):
assert_type(cb1.subgroup(ValueError), ExceptionGroup[ValueError] | None)
assert_type(cb2.subgroup((KeyError, TypeError)), ExceptionGroup[Exception] | None)


def cb_subgroup1(exc: SystemExit | CustomBaseGroup[SystemExit]) -> bool:
return True


def cb_subgroup2(exc: ValueError | CustomBaseGroup[ValueError]) -> bool:
return True


assert_type(cb1.subgroup(cb_subgroup1), BaseExceptionGroup[SystemExit] | None)
assert_type(cb2.subgroup(cb_subgroup2), BaseExceptionGroup[ValueError] | None)
cb1.subgroup(cb_subgroup2) # type: ignore
cb2.subgroup(cb_subgroup1) # type: ignore


# .split()
# --------

assert_type(
cb1.split(KeyboardInterrupt), tuple[BaseExceptionGroup[KeyboardInterrupt] | None, BaseExceptionGroup[SystemExit] | None]
)
assert_type(cb1.split(TypeError), tuple[ExceptionGroup[TypeError] | None, BaseExceptionGroup[SystemExit] | None])
assert_type(cb2.split((TypeError, IndexError)), tuple[ExceptionGroup[Exception] | None, BaseExceptionGroup[ValueError] | None])

assert_type(
cb2.split((TypeError, IndexError)), tuple[ExceptionGroup[Exception] | None, BaseExceptionGroup[ValueError] | None]
)

def cb_split1(exc: SystemExit | CustomBaseGroup[SystemExit]) -> bool:
return True


def cb_split2(exc: ValueError | CustomBaseGroup[ValueError]) -> bool:
return True


assert_type(cb1.split(cb_split1), tuple[BaseExceptionGroup[SystemExit] | None, BaseExceptionGroup[SystemExit] | None])
assert_type(cb2.split(cb_split2), tuple[BaseExceptionGroup[ValueError] | None, BaseExceptionGroup[ValueError] | None])
cb1.split(cb_split2) # type: ignore
cb2.split(cb_split1) # type: ignore


# .derive()
# ---------

Expand All @@ -304,22 +273,18 @@ def cb_split2(exc: ValueError | CustomBaseGroup[ValueError]) -> bool:
assert_type(cb2.derive([ValueError()]), ExceptionGroup[ValueError])
assert_type(cb2.derive([KeyboardInterrupt()]), BaseExceptionGroup[KeyboardInterrupt])


# ExceptionGroup Custom Subclass
# ==============================

_E = TypeVar("_E", bound=Exception)


class CustomGroup(ExceptionGroup[_E]):
...


CustomGroup("x", [SystemExit()]) # type: ignore
cg1 = CustomGroup("x", [ValueError()])
assert_type(cg1, CustomGroup[ValueError])


# .subgroup()
# -----------

Expand All @@ -329,39 +294,31 @@ class CustomGroup(ExceptionGroup[_E]):
assert_type(cg1.subgroup(ValueError), ExceptionGroup[ValueError] | None)
assert_type(cg1.subgroup((KeyError, TypeError)), ExceptionGroup[Exception] | None)


def cg_subgroup1(exc: ValueError | CustomGroup[ValueError]) -> bool:
return True


def cg_subgroup2(exc: ValueError) -> bool:
return True


assert_type(cg1.subgroup(cg_subgroup1), ExceptionGroup[ValueError] | None)
cg1.subgroup(cb_subgroup2) # type: ignore


# .split()
# --------

assert_type(cg1.split(TypeError), tuple[ExceptionGroup[TypeError] | None, ExceptionGroup[ValueError] | None])
assert_type(cg1.split((TypeError, IndexError)), tuple[ExceptionGroup[Exception] | None, ExceptionGroup[ValueError] | None])
cg1.split(BaseException) # type: ignore


def cg_split1(exc: ValueError | CustomGroup[ValueError]) -> bool:
return True


def cg_split2(exc: ValueError) -> bool:
return True


assert_type(cg1.split(cg_split1), tuple[ExceptionGroup[ValueError] | None, ExceptionGroup[ValueError] | None])
cg1.split(cg_split2) # type: ignore


# .derive()
# ---------

Expand Down