requiresthat 2025.6.19.4__py3-none-any.whl → 2025.6.21.0__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.
- requiresthat/_exceptions.py +1 -1
- requiresthat/_requires.py +54 -30
- {requiresthat-2025.6.19.4.dist-info → requiresthat-2025.6.21.0.dist-info}/METADATA +1 -1
- requiresthat-2025.6.21.0.dist-info/RECORD +8 -0
- requiresthat-2025.6.19.4.dist-info/RECORD +0 -8
- {requiresthat-2025.6.19.4.dist-info → requiresthat-2025.6.21.0.dist-info}/WHEEL +0 -0
- {requiresthat-2025.6.19.4.dist-info → requiresthat-2025.6.21.0.dist-info}/top_level.txt +0 -0
requiresthat/_exceptions.py
CHANGED
@@ -3,7 +3,7 @@ import textwrap
|
|
3
3
|
class RequirementNotFulfilledError(Exception):
|
4
4
|
"""Raise this when a requirement is found wanting"""
|
5
5
|
|
6
|
-
def __init__(self, that, when, subwhen=str(), msg=None):
|
6
|
+
def __init__(self, that, when, subwhen: str = str(), msg=None):
|
7
7
|
"""Show a default or a user-provided message indicating that some condition is unmet"""
|
8
8
|
|
9
9
|
if subwhen:
|
requiresthat/_requires.py
CHANGED
@@ -15,50 +15,56 @@ def requires(that, when: When = APRIORI) -> Optional[Callable]:
|
|
15
15
|
Fail if the condition is not met: do not invoke the callable or prevent the operation from being
|
16
16
|
considered a success.
|
17
17
|
|
18
|
-
Needs the
|
18
|
+
Needs the callable to be an instance method of a class, like so:
|
19
|
+
|
20
|
+
class C:
|
21
|
+
|
22
|
+
def __init__(self, data=None):
|
23
|
+
self.data = data
|
24
|
+
|
25
|
+
@requires(that='self.data is not None')
|
26
|
+
@requires(that='self.data == "spam"', when=APRIORI)
|
27
|
+
@requires(that='True is not False')
|
28
|
+
@requires(that='self.data != "spam"', when=POSTMORTEM)
|
29
|
+
@requires(that='len(self.data) >= 3', when=BEFOREANDAFTER)
|
30
|
+
def method(self):
|
31
|
+
self.data = 'ham'
|
32
|
+
|
33
|
+
X = C(data='spam')
|
34
|
+
X.method()
|
35
|
+
|
36
|
+
Try adding
|
37
|
+
|
38
|
+
the_impossible = '1 / 0'
|
39
|
+
and
|
40
|
+
@requires(the_impossible)
|
41
|
+
|
42
|
+
to the list of decorators above and watch what happens.
|
19
43
|
"""
|
20
|
-
def func_wrapper(
|
44
|
+
def func_wrapper(__func: Callable) -> Optional[Callable]:
|
21
45
|
"""First-level wrap the decoratee"""
|
22
46
|
|
23
|
-
@wraps(
|
47
|
+
@wraps(__func)
|
24
48
|
def inner_wrapper(self, *pargs, **kwargs) -> Optional[Callable]:
|
25
49
|
"""Wrap the first-level wrapper
|
26
50
|
|
27
51
|
The wrapping stops here...
|
28
52
|
"""
|
29
|
-
|
30
|
-
|
31
|
-
except AssertionError as exc:
|
32
|
-
raise NoCallableConstructError(func) from exc
|
53
|
+
if not callable(__func):
|
54
|
+
raise NoCallableConstructError(__func)
|
33
55
|
else:
|
34
|
-
# Since we want to give detailed sub-failure diax in case of BEFOREANDAFTER,
|
35
|
-
# economisng on the ifs below is tricky.
|
36
56
|
if when == APRIORI:
|
37
|
-
|
38
|
-
|
39
|
-
except AssertionError as exc:
|
40
|
-
raise RequirementNotFulfilledError(that, when) from exc
|
41
|
-
else:
|
42
|
-
func(self, *pargs, **kwargs)
|
57
|
+
__assert(self, that, when)
|
58
|
+
__func(self, *pargs, **kwargs)
|
43
59
|
|
44
60
|
elif when == POSTMORTEM:
|
45
|
-
|
46
|
-
|
47
|
-
assert eval(that)
|
48
|
-
except AssertionError as exc:
|
49
|
-
raise RequirementNotFulfilledError(that, when) from exc
|
61
|
+
__func(self, *pargs, **kwargs)
|
62
|
+
__assert(self, that, when)
|
50
63
|
|
51
64
|
elif when == BEFOREANDAFTER:
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
raise RequirementNotFulfilledError(that, when, APRIORI) from exc
|
56
|
-
else:
|
57
|
-
func(self, *pargs, **kwargs)
|
58
|
-
try:
|
59
|
-
assert eval(that)
|
60
|
-
except AssertionError as exc:
|
61
|
-
raise RequirementNotFulfilledError(that, when, POSTMORTEM) from exc
|
65
|
+
__assert(self, that, when, APRIORI)
|
66
|
+
__func(self, *pargs, **kwargs)
|
67
|
+
__assert(self, that, when, POSTMORTEM)
|
62
68
|
|
63
69
|
# We don't need an else clause; trying to enlist something that's not in the enum
|
64
70
|
# will be penalised with an AttributeError, and small typos will be healed with a
|
@@ -67,3 +73,21 @@ def requires(that, when: When = APRIORI) -> Optional[Callable]:
|
|
67
73
|
return inner_wrapper
|
68
74
|
|
69
75
|
return func_wrapper
|
76
|
+
|
77
|
+
def __assert(self, that, when: When, subwhen: str = str()):
|
78
|
+
"""Do the actual testing and raise the proper exceptions
|
79
|
+
|
80
|
+
The reason we don't use assert here is to avoid the Knuthian dilemma of premature optimisation;
|
81
|
+
namely, that it nukes this useful tool, :-[
|
82
|
+
"""
|
83
|
+
# We map everything that can go wrong to one exception; that way, the user has to deal with only
|
84
|
+
# one catagory of exceptions.
|
85
|
+
failure = RequirementNotFulfilledError(that, when, subwhen)
|
86
|
+
try:
|
87
|
+
if not eval(that):
|
88
|
+
raise failure from None
|
89
|
+
except:
|
90
|
+
raise failure from None
|
91
|
+
else:
|
92
|
+
# Success!
|
93
|
+
pass
|
@@ -0,0 +1,8 @@
|
|
1
|
+
requiresthat/__init__.py,sha256=VTyJru4K2_e7UEa0od6kXU7M8ovfD176GXEXqtNf-nY,154
|
2
|
+
requiresthat/_exceptions.py,sha256=kzzbS5F-h4Duj84lx6AXWUBHv56VD9jX8og_gJIvN9c,1282
|
3
|
+
requiresthat/_requires.py,sha256=XmJL6OQBDSNmS7xTRukk0-7vS98pbiTMQ9tEV0zVkOA,3222
|
4
|
+
requiresthat/_when.py,sha256=VoGuvoG9WDEMIPeKnxAsjMTaCMhgSszf4xxa1vLn5aU,299
|
5
|
+
requiresthat-2025.6.21.0.dist-info/METADATA,sha256=HGf8uPalVYizcliGDFsadP9U9kfomQOez7NNkfONCdw,2072
|
6
|
+
requiresthat-2025.6.21.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
7
|
+
requiresthat-2025.6.21.0.dist-info/top_level.txt,sha256=mUgMTpAG75GYtt5_rVajUyWp-O_1VrrkqRo_hY9L9So,13
|
8
|
+
requiresthat-2025.6.21.0.dist-info/RECORD,,
|
@@ -1,8 +0,0 @@
|
|
1
|
-
requiresthat/__init__.py,sha256=VTyJru4K2_e7UEa0od6kXU7M8ovfD176GXEXqtNf-nY,154
|
2
|
-
requiresthat/_exceptions.py,sha256=-Q9lhvHnSapP_UGMAnlXxwDaGcxagULIe5oP5h8a2IU,1275
|
3
|
-
requiresthat/_requires.py,sha256=a293jEksRYfF9SxdBqyzuEAO9JNh8HfX8Eg10eJqQpE,2783
|
4
|
-
requiresthat/_when.py,sha256=VoGuvoG9WDEMIPeKnxAsjMTaCMhgSszf4xxa1vLn5aU,299
|
5
|
-
requiresthat-2025.6.19.4.dist-info/METADATA,sha256=-LUMs7OOTqCdH4lkNxEgFrhSse4fkW-HZXe-p17o-eI,2072
|
6
|
-
requiresthat-2025.6.19.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
7
|
-
requiresthat-2025.6.19.4.dist-info/top_level.txt,sha256=mUgMTpAG75GYtt5_rVajUyWp-O_1VrrkqRo_hY9L9So,13
|
8
|
-
requiresthat-2025.6.19.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|