Programming by contract
Project description
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
- Automatic property-based tests.
- Static analysis.
- Generators and async coroutines support.
- External validators support.
- Type-annotated and mypy-friendly.
- Specify allowed exceptions for function.
- Invariant for all actions with class instances.
- Decorators to control available resources: forbid output, network operations, raising exceptions.
- You can disable contracts on production.
Available decorators
CLassic DbC:
- deal.pre -- validate function arguments (pre-condition)
- deal.post -- validate function return value (post-condition)
- deal.ensure -- post-condition that accepts not only result, but also function arguments.
- deal.inv -- validate object internal state (invariant).
Take more control:
- deal.module_load -- check contracts at module initialization.
- deal.offline -- forbid network requests
- deal.raises -- allow only list of exceptions
- deal.reason -- check function arguments that caused a given exception.
- deal.silent -- forbid output into stderr/stdout.
Helpers:
- deal.chain -- chain a few contracts in one.
- deal.pure -- forbid side-effects and combine
safe
,silent
, andoffline
. - deal.safe -- forbid exceptions.
Installation
python3 -m pip 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), message='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
self.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:
@deal.inv
controls that visits count in post always non-negative.@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.@deal.raises
says that only possible exception that can be raised isPostAlreadyLiked
.@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.deal.post
checks result format forget_state
. So, all external code can be sure that fieldslikes
andvisits
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 deal.readthedocs.io.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
deal-3.7.0.tar.gz
(20.2 kB
view details)
Built Distribution
deal-3.7.0-py3-none-any.whl
(28.9 kB
view details)
File details
Details for the file deal-3.7.0.tar.gz
.
File metadata
- Download URL: deal-3.7.0.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: DepHell/0.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b61b5ddc49616c94443c90af72b3c116397316dae311b38072cf7801ba70b9e3 |
|
MD5 | 5d8497bd7b8e339b5806346a8a0a608e |
|
BLAKE2b-256 | bc7bab38831ea6aef0dd734edaef8f0205bfabe4edda63eb339006be11f1c603 |
File details
Details for the file deal-3.7.0-py3-none-any.whl
.
File metadata
- Download URL: deal-3.7.0-py3-none-any.whl
- Upload date:
- Size: 28.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: DepHell/0.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5582c5d2753acceae2c104b5f61f3f281bc2b4c7990b56d27357603ace3325d2 |
|
MD5 | 79e1a2fbfc9dfa89e50d729fe6a9685f |
|
BLAKE2b-256 | bd804b8611008f725ef3c8c14e89732ff44a9f37d44f28716a0d44af569748d3 |