Skip to main content

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

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()

The that can be almost any valid Python statement which can be evaluated for its veracity, and whose result 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.

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.15.7.tar.gz (3.4 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.15.7-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: requiresthat-2025.6.15.7.tar.gz
  • Upload date:
  • Size: 3.4 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.15.7.tar.gz
Algorithm Hash digest
SHA256 3b948aaf786f3e139548a56ef30591fcfab47c107af940361b6ed6232976f20c
MD5 52891fd09c41fa04099d547abfdb9b7f
BLAKE2b-256 856db733c9f5132544ca39d2a46360f82377152e0da64d63754f67425a7d15a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for requiresthat-2025.6.15.7-py3-none-any.whl
Algorithm Hash digest
SHA256 70ae5b3db4e3d8da5e6d73fed64813b893430445d118725a0fd4ce16316963b3
MD5 1cae01340498388d30d772e3ec16eeba
BLAKE2b-256 48eb488499acb31d6420b2e8a65adfaf238c6401454b628fbda64acd098a4f69

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