File: check_types.py

package info (click to toggle)
python-exceptiongroup 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 264 kB
  • sloc: python: 2,425; makefile: 5
file content (36 lines) | stat: -rw-r--r-- 1,025 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from typing_extensions import assert_type

from exceptiongroup import BaseExceptionGroup, ExceptionGroup, catch, suppress

# issue 117
a = BaseExceptionGroup("", (KeyboardInterrupt(),))
assert_type(a, BaseExceptionGroup[KeyboardInterrupt])
b = BaseExceptionGroup("", (ValueError(),))
assert_type(b, BaseExceptionGroup[ValueError])
c = ExceptionGroup("", (ValueError(),))
assert_type(c, ExceptionGroup[ValueError])

# expected type error when passing a BaseException to ExceptionGroup
ExceptionGroup("", (KeyboardInterrupt(),))  # type: ignore[type-var]


# code snippets from the README


def value_key_err_handler(excgroup: BaseExceptionGroup) -> None:
    for exc in excgroup.exceptions:
        print("Caught exception:", type(exc))


def runtime_err_handler(exc: BaseExceptionGroup) -> None:
    print("Caught runtime error")


with catch(
    {(ValueError, KeyError): value_key_err_handler, RuntimeError: runtime_err_handler}
):
    ...


with suppress(RuntimeError):
    raise ExceptionGroup("", [RuntimeError("boo")])