Skip to main content

Support for requirements-assisted development

Project description

Decorate an instance method with pre- and/or postconditions that must be fulfilled

Example usage

from requiresthat import (
    requires,
    RequirementNotFulfilledError,
    APRIORI,
    POSTMORTEM,
    BEFOREANDAFTER,
)
class C:

    def __init__(self, data=None):
        self.data = data

    @requires(that='self.data is not None')
    @requires(that='self.data == "spam"', when=APRIORI)
    @requires(that='True is not False')
    @requires(that='self.data != "spam"', when=POSTMORTEM)
    @requires(that='len(self.data) >= 3', when=BEFOREANDAFTER)
    def method(self):
        self.data = 'ham'

X = C(data='spam')
X.method()

See the tests for more.

The that can be almost any valid Python statement which can be evaluated and whose veracity can be asserted, and the result thereof will decide whether or not the method fires/will be considered a success. Then details should be observed here.

The parameter when decides if the condition is a-priori, post-mortem, or before-and-after. The default is a-priori, meaning a precondition. Note that before-and-after does not mean during; you cannot mandate an invariant this way!

RequirementNotFulfilledError is the exception you have to deal with in case a condition is not met. NoCallableConstructError gets raised should you apply the decorator to a construct that is not callable. Both of these derive from the base class RequirementError.

Installation

The project is on PyPI, so simply run

python -m pip install requiresthat

If you want to cook it on your own, do this:

git clone https://gitlab.com/bedhanger/mwe.git
cd mwe/python/requiresthat
python -m venv --system-site-packages venv
source venv/bin/activate
python -m build
python -m pip install .
deactivate

It is recommended to carry out the build and install steps in a venv. This is shown in an exemplary manner in the snipped above that deals with building from scratch.

Running the tests

Build the package from scratch like shown above, but do not leave the venv (i.e., don’t call deactivate). Then

python -m pytest --verbose

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

requiresthat-2025.7.2.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requiresthat-2025.7.2.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file requiresthat-2025.7.2.0.tar.gz.

File metadata

  • Download URL: requiresthat-2025.7.2.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for requiresthat-2025.7.2.0.tar.gz
Algorithm Hash digest
SHA256 c332b27a6d99d2d4ced5e8e5ac22d41f29b8cfacc09943fb1dbe9cb1ee008a5c
MD5 f398b038e7ec85b73f5253f94ddd0b92
BLAKE2b-256 e867018f4812aa664b62e1d160709a008f977cab827f10ea200ef72d2fcd4804

See more details on using hashes here.

File details

Details for the file requiresthat-2025.7.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for requiresthat-2025.7.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04502c1883a9a5a6c0e5769e07a147e31bacd9c24f6bcd81cea032870ea21f07
MD5 1a64fc406cf49c5d87a01d7eafae628f
BLAKE2b-256 f2c594967c22344f855c48c511f6096a02a3240a3b460cd9715059c3bd930463

See more details on using hashes here.

Supported by

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