Skip to content

assert_checks

Value assertions with detailed failure reporting.

This module provides the tas.* check functions - a set of assertion helpers used all over the library and its tests to validate arguments, results and invariants.

Each check raises an AssertionError when its condition is not satisfied, logging a detailed failure message - including the source location of the failing check, the performed operation and the involved values - before raising.

Example
from py_misc_utils import assert_checks as tas

tas.check_eq(1, 1)

try:
  tas.check_eq(1, 2)
except AssertionError as e:
  # Message like: '1 == 2 failed from <file>:<line>: <source line>'
  assert '1 == 2 failed from' in str(e)

check(a, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is true.

All the checks raise plain AssertionError by default - pass a custom exception class to raise something else (e.g. a project-specific error).

Parameters:

Name Type Description Default
a

The value to check.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is not true.

Source code in py_misc_utils/assert_checks.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def check(a, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is true.

  All the checks raise plain AssertionError by default - pass a custom
  exception class to raise something else (e.g. a project-specific
  error).

  Args:
    a: The value to check.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is not true.
  """
  if not a:
    _report_fail(level, None, msg=msg, exception=exception)

check_fn(fn, *args, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a predicate function returns a true value.

The predicate is used as the operation in the failure message - e.g. <lambda>(-1) failed from <file>:<line>: <source line>.

Parameters:

Name Type Description Default
fn

The predicate function to call.

required
*args

The arguments to pass to the predicate function.

()
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the predicate function returns a false value.

Source code in py_misc_utils/assert_checks.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def check_fn(fn, *args, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a predicate function returns a true value.

  The predicate is used as the operation in the failure message -
  e.g. ``<lambda>(-1) failed from <file>:<line>: <source line>``.

  Args:
    fn: The predicate function to call.
    *args: The arguments to pass to the predicate function.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the predicate function returns a false value.
  """
  if not fn(*args):
    _report_fail(level, fn, *args, msg=msg, exception=exception)

check_fnres_eq(res, fn, *args, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a function returns an expected result.

Note the argument order: the expected result comes first, so that the function and its arguments can be passed positionally.

Parameters:

Name Type Description Default
res

The expected result.

required
fn

The function to call.

required
*args

The arguments to pass to the function.

()
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the function result differs from the expected result.

Source code in py_misc_utils/assert_checks.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def check_fnres_eq(res,
                   fn,
                   *args,
                   level=logging.ERROR,
                   msg=None,
                   exception=AssertionError):
  """Checks that a function returns an expected result.

  Note the argument order: the expected result comes first, so that the
  function and its arguments can be passed positionally.

  Args:
    res: The expected result.
    fn: The function to call.
    *args: The arguments to pass to the function.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the function result differs from the expected result.
  """
  if fn(*args) != res:
    _report_fail(level, fn, *args, res=res, res_op='==', msg=msg, exception=exception)

check_fnres_ne(res, fn, *args, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a function does not return a given result.

Parameters:

Name Type Description Default
res

The result the function must not return.

required
fn

The function to call.

required
*args

The arguments to pass to the function.

()
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the function result equals the given result.

Source code in py_misc_utils/assert_checks.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def check_fnres_ne(res,
                   fn,
                   *args,
                   level=logging.ERROR,
                   msg=None,
                   exception=AssertionError):
  """Checks that a function does not return a given result.

  Args:
    res: The result the function must not return.
    fn: The function to call.
    *args: The arguments to pass to the function.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the function result equals the given result.
  """
  if fn(*args) == res:
    _report_fail(level, fn, *args, res=res, res_op='!=', msg=msg, exception=exception)

check_is_none(a, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is None.

Parameters:

Name Type Description Default
a

The value to check.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is not None.

Source code in py_misc_utils/assert_checks.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
def check_is_none(a, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is None.

  Args:
    a: The value to check.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is not None.
  """
  if a is not None:
    _report_fail(level, '==', a, 'None', msg=msg, exception=exception)

check_is_not_none(a, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is not None.

Parameters:

Name Type Description Default
a

The value to check.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is None.

Source code in py_misc_utils/assert_checks.py
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def check_is_not_none(a, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is not None.

  Args:
    a: The value to check.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is None.
  """
  if a is None:
    _report_fail(level, '!=', a, 'None', msg=msg, exception=exception)

check_in(a, b, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is contained in a container.

Parameters:

Name Type Description Default
a

The value to check.

required
b

The container which must contain the value.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is not contained in the container.

Source code in py_misc_utils/assert_checks.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def check_in(a, b, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is contained in a container.

  Args:
    a: The value to check.
    b: The container which must contain the value.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is not contained in the container.
  """
  if a not in b:
    _report_fail(level, 'in', a, b, msg=msg, exception=exception)

check_eq(a, b, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that two values are equal.

Parameters:

Name Type Description Default
a

The first value.

required
b

The second value.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the values are not equal.

Source code in py_misc_utils/assert_checks.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def check_eq(a, b, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that two values are equal.

  Args:
    a: The first value.
    b: The second value.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the values are not equal.
  """
  if a != b:
    _report_fail(level, '==', a, b, msg=msg, exception=exception)

check_ne(a, b, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that two values are different.

Parameters:

Name Type Description Default
a

The first value.

required
b

The second value.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the values are equal.

Source code in py_misc_utils/assert_checks.py
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def check_ne(a, b, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that two values are different.

  Args:
    a: The first value.
    b: The second value.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the values are equal.
  """
  if a == b:
    _report_fail(level, '!=', a, b, msg=msg, exception=exception)

check_le(a, b, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is less than, or equal to, another one.

Parameters:

Name Type Description Default
a

The value to check.

required
b

The reference value.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is greater than the reference value.

Source code in py_misc_utils/assert_checks.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
def check_le(a, b, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is less than, or equal to, another one.

  Args:
    a: The value to check.
    b: The reference value.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is greater than the reference value.
  """
  if not (a <= b):
    _report_fail(level, '<=', a, b, msg=msg, exception=exception)

check_ge(a, b, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is greater than, or equal to, another one.

Parameters:

Name Type Description Default
a

The value to check.

required
b

The reference value.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is less than the reference value.

Source code in py_misc_utils/assert_checks.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
def check_ge(a, b, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is greater than, or equal to, another one.

  Args:
    a: The value to check.
    b: The reference value.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is less than the reference value.
  """
  if not (a >= b):
    _report_fail(level, '>=', a, b, msg=msg, exception=exception)

check_lt(a, b, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is less than another one.

Parameters:

Name Type Description Default
a

The value to check.

required
b

The reference value.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is not less than the reference value.

Source code in py_misc_utils/assert_checks.py
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def check_lt(a, b, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is less than another one.

  Args:
    a: The value to check.
    b: The reference value.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is not less than the reference value.
  """
  if not (a < b):
    _report_fail(level, '<', a, b, msg=msg, exception=exception)

check_gt(a, b, level=logging.ERROR, msg=None, exception=AssertionError)

Checks that a value is greater than another one.

Parameters:

Name Type Description Default
a

The value to check.

required
b

The reference value.

required
level

The logging level used to log the failure message.

ERROR
msg

An optional failure description added to the failure message.

None
exception

The exception class to raise (default: AssertionError).

AssertionError

Raises:

Type Description
exception

If the value is not greater than the reference value.

Source code in py_misc_utils/assert_checks.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
def check_gt(a, b, level=logging.ERROR, msg=None, exception=AssertionError):
  """Checks that a value is greater than another one.

  Args:
    a: The value to check.
    b: The reference value.
    level: The logging level used to log the failure message.
    msg: An optional failure description added to the failure message.
    exception: The exception class to raise (default: AssertionError).

  Raises:
    exception: If the value is not greater than the reference value.
  """
  if not (a > b):
    _report_fail(level, '>', a, b, msg=msg, exception=exception)