Astaticanalysis tool for Python that blurs the line between testing and type systems.
If you have functions with type annotationsand add some checks in the (defunct)PEPsyntax, CrossHair will attempt to find counterexamples for you :
CrossHair works by repeatedly calling your functions with symbolic inputs. It uses anSMT solverto explore viable execution paths and find counterexamples for you. This is not a new idea; an approach for Python was first described inthis paper
. However, to my knowledge, CrossHair is the most complete implementation of the idea: it supports symbolic lists, dictionaries, sets, and custom / mutable objects.
NOTE:CrossHair is in a highly experimental state right now. You can help though – keep reading!
****************************************************Why Should I Use CrossHair?
More precision.Commonly, we care about more than just the type. Is it really any integer, or is it apositiveinteger? Is it any list, or does it have to be a non-empty list? CrossHair gives you that precision:
Interprocedural analysis.) CrossHair (1) validates the pre-conditions of called functions and (2) uses post-conditions of called functions to help it prove post-conditions in the caller.
Verify across all implementations.Contracts are particularly helpful when applied to base classes / interfaces: all implementations will be verified against them:
Catch errors. Setting a trivial post-condition of “True” is enough to enable analysis, which will find exceptions like index out-of-bounds errors:
**********
Support your type checker.CrossHair is a nice companion tomypy. Assert statements divide work between the two systems:
Optimize with Confidence.Using post-conditions, CrossHair ensures that optimized code continues to behave like equivalent naive code:
Cover doctest’s blind spots.Doctestis great for illustrative examples and CrossHair can document behavior more holistically. Some kinds of projects may be able to skip unittest / pytest entirely.
**********************************
******************** How to Write Contracts**************************
CrossHair largely follows thePEP 656
syntax for expressing “contracts. ” In short:
- Declare your post-conditions (what you expect to be true of the function’s return value) like this: (******************************************************post: __return__>0
************************************
GIPHY App Key not set. Please check settings