A Python library for design by contract (DbC) and checking values, exceptions, and side-effects. In a nutshell, deal empowers you to write bug-free code. By adding a few decorators to your code, you get for free tests, static analysis, formal verification, and much more. Read intro to get started.
Features
- Classic DbC: precondition, postcondition, invariant.
- Tracking exceptions and side-effects.
- Property-based testing.
- Static checker.
- Integration with pytest, flake8, sphinx, and hypothesis.
- Type annotations support.
- External validators support.
- Contracts for importing modules.
- Can be enabled or disabled on production.
- Colorless: annotate only what you want. Hence, easy integration into an existing project.
- Colorful: syntax highlighting for every piece of code in every command.
- Memory leaks detection: deal makes sure that pure functions don't leave unexpected objects in the memory.
- DRY: test discovery, error messages generation.
- Partial execution: linter executes contracts to statically check possible values.
- Formal verification: prove that your code works for all input (or find out when it doesn't).
- Zero-dependency runtime: there are some dependencies for analysis tools, but nothing of it is required on the production.
- Fast: each code change is benchmarked and profiled.
- Reliable: the library has 100% test coverage, partially verified, and runs on production by multiple companies since 2018.
Deal in 30 seconds
@deal.post(lambda result: result >= 0)
@deal.pure
def count(items: List[str], item: str) -> int:
return items.count(item)
test_count = deal.cases(count)
Now we can:
- Run
python3 -m deal lint
or flake8
to statically check errors. - Run
python3 -m deal test
or pytest
to generate and run tests. - Just use the function in the project and check errors in runtime.
Read more in the documentation.
Installation
python3 -m pip install --user 'deal[all]'
Contributing
Contributions are welcome! A few ideas what you can contribute:
- Add new checks for the linter.
- Improve documentation.
- Add more tests.
- Improve performance.
- Found a bug? Fix it!
- Made an article about deal? Great! Let's add it into the
README.md
. - Don't have time to code? No worries! Just tell your friends and subscribers about the project. More users -> more contributors -> more cool features.
To run tests locally, all you need is task. Run task all
to run all code formatters, linters, and tests.
Thank you :heart: