Skip to main content

Programming by contract

Project description

Deal
Build Status Coverage Status PyPI version Development Status Code size License

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.

Features

  • 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

Installation

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):
    pass

@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), 'invalid username format')
    @deal.raises(PostAlreadyLiked)
    @deal.chain(deal.offline, deal.silent)
    def like(self, user: str) -> None:
        if user in self.likes:
            raise PostAlreadyLiked
        likes.add(user)

    @deal.post(lambda result: 'visits' in result)
    @deal.post(lambda result: 'likes' in result)
    @deal.post(lambda result: result['likes'] > 0)
    @deal.pure
    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. deal.post 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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deal-2.3.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

deal-2.3.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file deal-2.3.0.tar.gz.

File metadata

  • Download URL: deal-2.3.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for deal-2.3.0.tar.gz
Algorithm Hash digest
SHA256 8ca9c8bcd7a5b93283d0750095849898b5ce5917cd967bb0e1d75f5caf542109
MD5 86b90d9ddd953af47299ad6d75791278
BLAKE2b-256 1806da65205cf265a16224d4597f35fa2b515e8e646c49efc1885796a57a5231

See more details on using hashes here.

File details

Details for the file deal-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: deal-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for deal-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5c232b12100e9d87dd27318d8466b76d6004d9f5bf4a28b2271a69bb2284e1d
MD5 fc35169542aea49aac507e23ab68103f
BLAKE2b-256 11a6e785d52ab0ba2216a0eaa225d0afb65cf27fedc5fdef4f727e437258ffa4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page