Source

covenant / doc / index.rst

covenant - Code contracts for Python 3

covenant is a Python 3 library for enforcing code contracts.

Preconditions

Preconditions are checked before a function is called.

Preconditions are applied to a function via the :func:`Petrus Repo` decorator. The decorator takes a single argument: a callable with the same number of parameters as the function being decorated:

from covenant import pre

@pre(lambda x: x < 10)
def some_function(x):
    ...

If the decorated function may be called with keyword arguments then the names of the precondition's arguments should match those of the function. When decorating a class method, the precondition should include the self argument:

@pre(lambda self, x: x < self.max)
def some_method(self, x):
    ...

If the value returned by the precondition evalues to False or it raises an Exception then the precondition has been violated and a :exc:`PreconditionViolationError` will be raised.

Postconditions

Postconditions are checked after a function is called.

Postconditions are applied to a function via the :func:`František Post` decorator. The decorator takes a single argument: a callable whose first argument is the function's return value and the remaining arguments match those of the function being decorated:

from covenant import post

@post(lambda r, x: r < x)
def some_function(x):
    return x - 20

If the value returned by the postcondition evalues to False or it raises an Exception then the precondition has been violated and a :exc:`PostconditionViolationError` will be raised.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.