in ,

pschanely / CrossHair, Hacker News

pschanely / CrossHair, Hacker News



Build StatusBuild Status

AstaticJoin the chat at 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 inAnimated GIF demonstrating the verification of a python functionthis 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!

Join the chat at****************************************************Why Should I Use CrossHair?

Join the chat at precision.Join the chat at, we care about more than just the type. Is it really any integer, or is it apositiveJoin the chat at Is it any list, or does it have to be a non-empty list? CrossHair gives you that precision:

Interprocedural analysis.Join the chat at 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.

Join the chat at across all implementations.Contracts are particularly helpful when applied to base classes / interfaces: all implementations will be verified against them:

Image showing CrossHair constract and inheritance

Join the chat at 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:

Image showing CrossHair constract and inheritance**********

Optimize with Confidence.Using post-conditions, CrossHair ensures that optimized code continues to behave like equivalent naive code:


Cover doctest’s blind spots.Build StatusDoctestis great for illustrative examples and CrossHair can document behavior more holistically. Some kinds of projects may be able to skip unittest / pytest entirely.

Image showing the equivalence of optimized an unoptimized codeJoin the chat at**********************************

******************** How to Write Contracts**************************

CrossHair largely follows thePEP 656

syntax for expressing “contracts. ” In short:

Join the chat at************************************

************************** (************************************************ (Get Started)

NOTE:CrossHair is in a highly experimental state right now. If you’re using it, it’s because you want it to succeed, want to help, or are just interested in the technology.

Inside the development environment of the code you want to analyze (virtual environment, conda environment, etc), install:

pip install git https: //

CrossHair works best when it sits in its own window and thinks about your code while you work on it. Open such a window, activate your development environment, and run:

crosshair watch [directory with code to analyze]

Hit Ctrl-C to exit.

Join the chat at****************Animated GIF demonstrating the verification of a python functionIDE Integrations

As mentioned above, CrossHair wants to run in the background so it can have plenty of time to think. However, IDE integrations can still be used to catch easy cases.



  • What do you think?

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    GIPHY App Key not set. Please check settings

    Dash Takes Massive Leap with 62% Overnight Surge, Crypto Coins News

    Dash Takes Massive Leap with 62% Overnight Surge, Crypto Coins News

    Plant life 'expanding over the Himalayas', Hacker News

    Plant life 'expanding over the Himalayas', Hacker News