
For more info, please visit the GitHub page of this project.
Basic example
import parameter_checks as pc
@pc.hints.cleanup
@pc.hints.enforce
def div(a: int, b: pc.annotations.Checks[int, lambda b: b != 0]):
return a / b
div(1, 1)
div(1, 0)
As can be seen in this example, this package provides a new type-annotation:
pc.annotations.Checks
(it also provides pc.annotations.Hooks, see below).
Using @pc.hints.enforce on a function
will enforce the checks given to those annotations (but not the types).
@pc.hints.cleanup would produce the
div.__annotations__
of {"a": int, "b": int}
in the example above.
Complex example
import parameter_checks as pc
def decision_boundary(fct, parameter, parameter_name, typehint):
if type(parameter) is not typehint:
err_str = f"In function {fct}, parameter {parameter_name}={parameter} " \
f"is not of type {typehint}!"
raise TypeError(err_str)
return 3 + 4 * parameter - parameter ** 2
@pc.hints.enforce
def classify(
x: pc.annotations.Hooks[float, decision_boundary],
additional_offset: pc.annotations.Checks[float, lambda b: 0 <= b <= 100] = 0.
) -> bool:
return (x + additional_offset) >= 0
if __name__ == '__main__':
assert classify(1.) is True
assert classify(2.) is True
assert classify(5.) is False
assert classify(5., 2.) is True
classify("not a float!")
classify(1., -1.)
For additional comments on this example (and much more),
visit the GitHub page of this project.