File: exceptions.py

package info (click to toggle)
python-utils 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 396 kB
  • sloc: python: 2,135; makefile: 19; sh: 5
file content (44 lines) | stat: -rw-r--r-- 1,135 bytes parent folder | download
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
37
38
39
40
41
42
43
44
"""
This module provides utility functions for raising and reraising exceptions.

Functions:
    raise_exception(exception_class, *args, **kwargs):
        Returns a function that raises an exception of the given type with
        the given arguments.

    reraise(*args, **kwargs):
        Reraises the current exception.
"""

from . import types


def raise_exception(
    exception_class: types.Type[Exception],
    *args: types.Any,
    **kwargs: types.Any,
) -> types.Callable[..., None]:
    """
    Returns a function that raises an exception of the given type with the
    given arguments.

    >>> raise_exception(ValueError, 'spam')('eggs')
    Traceback (most recent call last):
        ...
    ValueError: spam
    """

    def raise_(*args_: types.Any, **kwargs_: types.Any) -> types.Any:
        raise exception_class(*args, **kwargs)

    return raise_


def reraise(*args: types.Any, **kwargs: types.Any) -> types.Any:
    """
    Reraises the current exception.

    This function seems useless, but it can be useful when you need to pass
    a callable to another function that raises an exception.
    """
    raise