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.

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.

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.6.19.1.tar.gz (3.9 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.6.19.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for requiresthat-2025.6.19.1.tar.gz
Algorithm Hash digest
SHA256 9d0fe91472df26b683e60e6e08c71a32956db20bd9ff4e2d3fe63e252486b84c
MD5 2b7b802c812b827530369d3eed1f9872
BLAKE2b-256 093c3b983ed628051580b1bcd7586c93c6d436e1f12c2e7999e79d4fb989ec89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for requiresthat-2025.6.19.1-py3-none-any.whl
Algorithm Hash digest
SHA256 db9a5cba514dea083985f89f92a922d5dab5490c8fd79cac2f8246596845cb21
MD5 91b890ab60ed83db6e443e1f3a10d014
BLAKE2b-256 bef3d6b37752ef81fdef38b065396dff83f6c108ca94df393f486434a63dc457

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