Skip to content
Design by contract for Python with many validators support.
Python Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore improve typing a little bit Oct 4, 2019
LICENSE Updates readme with the correct links Oct 6, 2019


Build Status Coverage Status PyPI version Development Status Code size

Deal -- python library for design by contract (DbC) programming.

That's nice assert statements in decorators style to validate function input, output, available operations and object state. Goal is make testing much easier and detect errors in your code that occasionally was missed in tests.


  • Functional declaration.
  • Custom exceptions.
  • Raising exceptions from contract.
  • Django Forms styled validators.
  • Attribute setting invariant validation.
  • Dynamically assigned attributes and methods invariant validation.
  • Decorators to control available resources: forbid input/output, network operations, raising exceptions

Available decorators

CLassic DbC:

  • @deal.pre -- validate function arguments (pre-condition)
  • -- validate function return value (post-condition)
  • @deal.inv -- validate object internal state (invariant)

Take more control:

  • @deal.offline -- forbid network requests
  • @deal.raises -- allow only list of exceptions
  • -- forbid exceptions
  • @deal.silent -- forbid output into stderr/stdout.


pip3 install --user deal

Quick Start

import re

import attr
import deal

REX_LOGIN = re.compile(r'^[a-zA-Z][a-zA-Z0-9]+$')

class PostAlreadyLiked(Exception):

@deal.inv(lambda post: post.visits >= 0)
class Post:
    visits: int = attr.ib(default=0)
    likes: set = attr.ib(factory=set)

    @deal.pre(lambda user: REX_LOGIN.match(user), message='invalid username format')
    @deal.chain(deal.offline, deal.silent)
    def like(self, user: str) -> None:
        if user in self.likes:
            raise PostAlreadyLiked
        self.likes.add(user) result: 'visits' in result) result: 'likes' in result) result: result['likes'] > 0)
    def get_state(self):
        return dict(visits=self.visits, likes=len(self.likes))

Now, Deal controls conditions and states of the object at runtime:

  1. @deal.inv controls that visits count in post always non-negative.
  2. @deal.pre checks user name format. We assume that it should be validated somewhere before by some nice forms with user-friendly error messages. So, if we have invalid login passed here, it's definitely developer's mistake.
  3. @deal.raises says that only possible exception that can be raised is PostAlreadyLiked.
  4. @deal.chain(deal.offline, deal.silent) controls that function has no network requests and has no output in stderr or stdout. So, if we are making unexpected network requests somewhere inside, deal let us know about it.
  5. checks result format for get_state. So, all external code can be sure that fields likes and visits always represented in the result and likes always positive.

If code violates some condition, sub-exception of deal.ContractError will be raised:

p = Post()
p.visits = -1
# InvContractError:

Dive deeper on

You can’t perform that action at this time.