requiresthat 2025.6.15.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ from ._requires import requires, RequirementNotFulfilledError, APRIORI, POSTMORTEM, BEFOREANDAFTER
@@ -0,0 +1,15 @@
1
+ """Raise this when a requirement is found wanting"""
2
+
3
+ import textwrap
4
+
5
+ class RequirementNotFulfilledError(Exception):
6
+
7
+ def __init__(self, that, when, msg=None):
8
+ """Show a default or a user-provided message indicating that some condition is unmet"""
9
+
10
+ self.default_msg = textwrap.dedent(f"""
11
+ {that!r} ({when.name!r}) does not hold
12
+ """).strip()
13
+
14
+ # Call the base class' constructor to init the exception class
15
+ super().__init__(msg or self.default_msg)
@@ -0,0 +1,40 @@
1
+ """See the README file"""
2
+
3
+ from typing import Optional, Callable
4
+ from functools import wraps
5
+
6
+ from ._when import When, APRIORI, POSTMORTEM, BEFOREANDAFTER
7
+ from ._exceptions import RequirementNotFulfilledError
8
+
9
+ def requires(that, when: When = APRIORI) -> Optional[Callable]:
10
+ """Require <that> of the decoratee, and require it <when>"""
11
+
12
+ def func_wrapper(func: Callable) -> Optional[Callable]:
13
+ """First-level wrap the decoratee"""
14
+
15
+ @wraps(func)
16
+ def inner_wrapper(self, *pargs, **kwargs) -> Optional[Callable]:
17
+ """Wrap the first-level wrapper
18
+
19
+ The wrapping stops here...
20
+ """
21
+ try:
22
+ if when == APRIORI:
23
+ assert eval(that)
24
+ # We can use a return here :-)
25
+ return func(self, *pargs, **kwargs)
26
+ elif when == POSTMORTEM:
27
+ func(self, *pargs, **kwargs)
28
+ assert eval(that)
29
+ elif when == BEFOREANDAFTER:
30
+ assert eval(that)
31
+ func(self, *pargs, **kwargs)
32
+ assert eval(that)
33
+ # We don't need an else clause; trying to enlist something that's not in the enum
34
+ # will be penalised with an AttributeError, and small typos will be healed with a
35
+ # suggestion as to what you might have meant.
36
+ except AssertionError as exc:
37
+ raise RequirementNotFulfilledError(that, when) from exc
38
+ return inner_wrapper
39
+
40
+ return func_wrapper
requiresthat/_when.py ADDED
@@ -0,0 +1,13 @@
1
+ """When should a condition hold"""
2
+
3
+ from enum import Enum, auto
4
+
5
+ class When(Enum):
6
+ APRIORI = auto()
7
+ POSTMORTEM = auto()
8
+ BEFOREANDAFTER = auto()
9
+ # There is no DURING or INBETWEEN!
10
+
11
+ APRIORI = When.APRIORI
12
+ POSTMORTEM = When.POSTMORTEM
13
+ BEFOREANDAFTER = When.BEFOREANDAFTER
@@ -0,0 +1,45 @@
1
+ Metadata-Version: 2.4
2
+ Name: requiresthat
3
+ Version: 2025.6.15.6
4
+ Summary: Decorate an instance method with pre- and/or postconditions that must be fulfilled
5
+ Author-email: Ann T Ropea <bedhanger@gmx.de>
6
+ Project-URL: Homepage, https://gitlab.com/bedhanger/mwe/-/tree/master/python/requiresthat
7
+ Description-Content-Type: text/x-rst
8
+
9
+ requiresthat
10
+ ============
11
+
12
+ Decorate an instance method with pre- and/or postconditions that must be fulfilled
13
+
14
+ Example usage
15
+ -------------
16
+
17
+ .. code-block:: python
18
+
19
+ from requiresthat import requires, RequirementNotFulfilledError, APRIORI, POSTMORTEM, BEFOREANDAFTER
20
+
21
+ class C:
22
+
23
+ def __init__(self, data=None):
24
+ self.data = data
25
+
26
+ @requires(that='self.data is not None')
27
+ @requires(that='self.data == "spam"', when=APRIORI)
28
+ @requires(that='True is not False')
29
+ @requires(that='self.data != "spam"', when=POSTMORTEM)
30
+ @requires(that='len(self.data) >= 3', when=BEFOREANDAFTER)
31
+ def method(self):
32
+ self.data = 'ham'
33
+
34
+ X = C(data='spam')
35
+ X.method()
36
+
37
+ The ``that`` can be almost any valid Python statement which can be evaluated for its veracity, and
38
+ whose result will decide whether or not the method fires/will be considered a success.
39
+
40
+ The parameter ``when`` decides if the condition is a-priori, post-mortem, or before-and-after.
41
+ The default is a-priori, meaning a precondition. Note that before-and-after does *not* mean during;
42
+ you cannot mandate an invariant this way!
43
+
44
+ ``RequirementNotFulfilledError`` is the exception you have to deal with in case a condition is not
45
+ met.
@@ -0,0 +1,8 @@
1
+ requiresthat/__init__.py,sha256=-pvz61UhmWhwzjbKTHQ83_YqZx1MXQEvqSgb7Q0d9jE,99
2
+ requiresthat/_exceptions.py,sha256=bgrTgn3JCJv_KbYIpaghZ8v-SP5fiOpzeFC5zB8uq8M,504
3
+ requiresthat/_requires.py,sha256=F28tp_3L_E06ERsSGkPx-YrbBolmGO1NymJZINtocts,1562
4
+ requiresthat/_when.py,sha256=yQWG7qGzv0iEsLA7y0vMkbpHi4Jsby8huOzea2BG_c8,285
5
+ requiresthat-2025.6.15.6.dist-info/METADATA,sha256=k75QG862vBm3chzLGPXdGi2X029YxF406N7GNyJoQaQ,1588
6
+ requiresthat-2025.6.15.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ requiresthat-2025.6.15.6.dist-info/top_level.txt,sha256=mUgMTpAG75GYtt5_rVajUyWp-O_1VrrkqRo_hY9L9So,13
8
+ requiresthat-2025.6.15.6.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ requiresthat