Design by Contract framework for the Python programming language

OData support
Dr. Asztalos Márk
Department of Automation and Applied Informatics

The software design approach called Design by Contract was created by Bertrand Meyer in the 1980s, when he designed the Eiffel programming language. This methodology prescribes, that software designers should design software component interfaces with formal, precise and verifiable specifications. The interface of a class should contain preconditions and postconditions for every method, and optionally an invariant for the class. This specification is called the contract, analogous to business contracts. The two parties are the class and its clients. The class is obligated to guarantee the postconditions (and the invariant), if the clients satisfy the preconditions. Therefore, there is a clear separation between the rights and obligations of the parties.

Python is a general purpose, high level programming language, which was designed with emphasis on code readability. It primarily supports object oriented programming, but it has some support for functional programming. Its type system is dynamic and strong. Most elements of the language (functions, classes) are first-class objects and can be modified runtime. This dynamic nature of the language along with the metaclasses support metaprogramming. Python supports multiple inheritance and operator overloading.

My goal was to implement a framework supporting Design by Contract for the Python programming language. The implementation makes the usage of the elements of Design by Contract possible: defining preconditions, postconditions and invariants for Python classes. It also verifies these conditions. It was a requirement, that the implementation must not modify the Python interpreter. Hence, it is usable as a module on a regular Python installation.

The completed module enables a programmer to use Design by Contract for developing Python programs. One can define preconditions, postconditions and invariants, even in a multiple inheritance situation. The verification of the conditions is performed runtime, or it can be disabled if necessary. Besides these features it implements a runtime duck typing type checking algorithm. The module is written in pure Python, so it is usable on a standard Python (3.1 or later) interpreter. By using this module, the programmer can employ Design by Contract when using the Python programming language.


Please sign in to download the files of this thesis.