AssertionLib
A package for performing assertions and providing informative exception messages.
Installation
PyPi: pip install AssertionLib
GitHub: pip install git+https://github.com/nlesc-nano/AssertionLib
Usage
A comprehensive overview of all available assertion methods is provided in the documentation. A few examples of some basic assertion:
>>> import numpy as np
>>> from assertionlib import assertion
# Assert the output of specific callables
>>> assertion.eq(5, 5) # 5 == 5
>>> assertion.lt(5, 6) # 5 < 6
>>> assertion.gt(6, 5) # 5 > 6
>>> assertion.isinstance(5, int)
>>> assertion.hasattr(5, '__init__')
>>> assertion.any([False, False, True])
>>> assertion.isfinite(1.0)
# Simply assert a value
>>> assertion(5 == 5)
>>> assertion(isinstance(5, int))
# Apply post-processing before conducting the assertion
>>> ar_large = np.ones(10)
>>> ar_small = np.zeros(10)
>>> assertion.gt(ar_large, ar_small, post_process=np.all) # all(ar_large > ar_small)
# Perform an assertion which will raise an AssertionError
>>> assertion.eq(5, 6, message='Fancy custom error message') # 5 == 6
Traceback (most recent call last):
...
AssertionError: output = eq(a, b); assert output
exception: AssertionError = AssertionError('Fancy custom error message')
output: bool = False
a: int = 5
b: int = 6
A few examples of AssertionErrors raised due to incorrect method signatures:
>>> from assertionlib import assertion
>>> assertion.len(5)
Traceback (most recent call last):
...
AssertionError: output = len(obj); assert output
exception: TypeError = TypeError("object of type 'int' has no len()")
output: NoneType = None
obj: int = 5
>>> from assertionlib import assertion
>>> assertion.eq(5, 5, 5, 5)
Traceback (most recent call last):
...
AssertionError: output = eq(a, b, _a, _b); assert output
exception: TypeError = TypeError('eq expected 2 arguments, got 4')
output: NoneType = None
a: int = 5
b: int = 5
_a: int = 5
_b: int = 5
A demonstration of the exception parameter. Providing an exception type will assert that the provided exception is raised during/before the assertion process:
>>> from assertionlib import assertion
>>> len(5)
Traceback (most recent call last):
...
TypeError: object of type 'int' has no len()
>>> from assertionlib import assertion
>>> assertion.len(5, exception=TypeError) # i.e. len(5) should raise a TypeError
>>> assertion.len([5], exception=TypeError)
Traceback (most recent call last):
...
AssertionError: output = len(obj); assert output
exception: AssertionError = AssertionError("Failed to raise 'TypeError'")
output: int = 1
obj: list = [5]
Lastly, the output of custom callables can be asserted in one of the following two ways, supplying the callable to AssertionManager.assert() or creating a custom assertion method and adding it to an instance with AssertionManager.add_to_instance():
>>> from assertionlib import assertion
>>> def my_fancy_func(a: object) -> bool:
... return False
# Approach #1, supply to-be asserted callable to assertion.assert_()
>>> assertion.assert_(my_fancy_func, 5)
Traceback (most recent call last):
...
AssertionError: output = my_fancy_func(a); assert output
exception: AssertionError = AssertionError(None)
output: bool = False
a: int = 5
>>> from assertionlib import assertion
# Approach #2, permanantly add a new bound method using assertion.add_to_instance()
>>> assertion.add_to_instance(my_fancy_func)
>>> assertion.my_fancy_func(5)
Traceback (most recent call last):
...
AssertionError: output = my_fancy_func(a); assert output
exception: AssertionError = AssertionError(None)
output: bool = False
a: int = 5
Metadata
Release files for AssertionLib 3.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| AssertionLib-3.2.2.tar.gz | 38.6 kB | Without provenance | Details for AssertionLib-3.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| AssertionLib-3.2.2-py3-none-any.whl | Python 3 | none | any | Details for AssertionLib-3.2.2-py3-none-any.whl |
Total release size: 71.7 kB
Release files / AssertionLib-3.2.2.tar.gz
| Download URL | AssertionLib-3.2.2.tar.gz |
|---|---|
| Size | 38.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b018919ceb34c815cfc303cf48c9d10461f6e6bbe90cf31618ea57a336a33965
|
|
BLAKE2b-256 checksum How to use checksums |
3d73fda51a469c141c8c70e4d664167198e12320d5d277072b27d6652d887232
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.3
|
Release files / AssertionLib-3.2.2-py3-none-any.whl
| Download URL | AssertionLib-3.2.2-py3-none-any.whl |
|---|---|
| Size | 33.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0d08f778fc378bb2231072b9448e07d8274e0281729b984a816c40158055939
|
|
BLAKE2b-256 checksum How to use checksums |
cb62fce3b5daafe3e444af09bacf1805ba551e9b1667ace511de49cd577e0b3c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.3
|
Security for AssertionLib 3.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.