/
custom_assert.py
67 lines (48 loc) · 2.32 KB
/
custom_assert.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Provide some variants of assert."""
from typing import Type
def _custom_assert(
condition: bool, on_error_msg: str = "", error_type: Type[Exception] = AssertionError
):
"""Provide a custom assert which is kept even if the optimized Python mode is used.
See https://docs.python.org/3/reference/simple_stmts.html#assert for the documentation
on the classical assert function
Args:
condition(bool): the condition. If False, raise AssertionError
on_error_msg(str): optional message for precising the error, in case of error
error_type (Type[Exception]): the type of error to raise, if condition is not fulfilled.
Default to AssertionError
Raises:
error_type: Raises an error if condition is False
"""
if not condition:
raise error_type(on_error_msg)
def assert_true(
condition: bool, on_error_msg: str = "", error_type: Type[Exception] = AssertionError
):
"""Provide a custom assert to check that the condition is True.
Args:
condition(bool): the condition. If False, raise AssertionError
on_error_msg(str): optional message for precising the error, in case of error
error_type (Type[Exception]): the type of error to raise, if condition is not fulfilled.
Default to AssertionError
"""
_custom_assert(condition, on_error_msg, error_type)
def assert_false(
condition: bool, on_error_msg: str = "", error_type: Type[Exception] = AssertionError
):
"""Provide a custom assert to check that the condition is False.
Args:
condition(bool): the condition. If True, raise AssertionError
on_error_msg(str): optional message for precising the error, in case of error
error_type (Type[Exception]): the type of error to raise, if condition is not fulfilled.
Default to AssertionError
"""
_custom_assert(not condition, on_error_msg, error_type)
def assert_not_reached(on_error_msg: str, error_type: Type[Exception] = AssertionError):
"""Provide a custom assert to check that a piece of code is never reached.
Args:
on_error_msg(str): message for precising the error
error_type (Type[Exception]): the type of error to raise, if condition is not fulfilled.
Default to AssertionError
"""
_custom_assert(False, on_error_msg, error_type)