requiresthat 2025.6.19.3__tar.gz → 2025.6.20.0__tar.gz
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-2025.6.19.3 → requiresthat-2025.6.20.0}/PKG-INFO +1 -1
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/pyproject.toml +1 -1
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat/_exceptions.py +1 -1
- requiresthat-2025.6.20.0/src/requiresthat/_requires.py +87 -0
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat.egg-info/PKG-INFO +1 -1
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/tests/test_requiresthat.py +3 -3
- requiresthat-2025.6.19.3/src/requiresthat/_requires.py +0 -62
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/README.rst +0 -0
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/setup.cfg +0 -0
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat/__init__.py +0 -0
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat/_when.py +0 -0
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat.egg-info/SOURCES.txt +0 -0
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat.egg-info/dependency_links.txt +0 -0
- {requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat.egg-info/top_level.txt +0 -0
@@ -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:
|
@@ -0,0 +1,87 @@
|
|
1
|
+
"""See the `README
|
2
|
+
<https://gitlab.com/bedhanger/mwe/-/blob/master/python/requiresthat/README.rst>`_ file
|
3
|
+
"""
|
4
|
+
from typing import Optional, Callable
|
5
|
+
from functools import wraps
|
6
|
+
|
7
|
+
from ._when import When, APRIORI, POSTMORTEM, BEFOREANDAFTER
|
8
|
+
from ._exceptions import RequirementNotFulfilledError, NoCallableConstructError
|
9
|
+
|
10
|
+
def requires(that, when: When = APRIORI) -> Optional[Callable]:
|
11
|
+
"""Require ``that`` of the decoratee, and require it ``when``
|
12
|
+
|
13
|
+
Fail if the associated construct is not callable.
|
14
|
+
|
15
|
+
Fail if the condition is not met: do not invoke the callable or prevent the operation from being
|
16
|
+
considered a success.
|
17
|
+
|
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.
|
43
|
+
"""
|
44
|
+
def func_wrapper(__func: Callable) -> Optional[Callable]:
|
45
|
+
"""First-level wrap the decoratee"""
|
46
|
+
|
47
|
+
@wraps(__func)
|
48
|
+
def inner_wrapper(self, *pargs, **kwargs) -> Optional[Callable]:
|
49
|
+
"""Wrap the first-level wrapper
|
50
|
+
|
51
|
+
The wrapping stops here...
|
52
|
+
"""
|
53
|
+
if not callable(__func):
|
54
|
+
raise NoCallableConstructError(__func)
|
55
|
+
else:
|
56
|
+
if when == APRIORI:
|
57
|
+
__assert(self, that, when)
|
58
|
+
__func(self, *pargs, **kwargs)
|
59
|
+
|
60
|
+
elif when == POSTMORTEM:
|
61
|
+
__func(self, *pargs, **kwargs)
|
62
|
+
__assert(self, that, when)
|
63
|
+
|
64
|
+
elif when == BEFOREANDAFTER:
|
65
|
+
__assert(self, that, when, APRIORI)
|
66
|
+
__func(self, *pargs, **kwargs)
|
67
|
+
__assert(self, that, when, POSTMORTEM)
|
68
|
+
|
69
|
+
# We don't need an else clause; trying to enlist something that's not in the enum
|
70
|
+
# will be penalised with an AttributeError, and small typos will be healed with a
|
71
|
+
# suggestion as to what you might have meant.
|
72
|
+
|
73
|
+
return inner_wrapper
|
74
|
+
|
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
|
+
try:
|
84
|
+
if not eval(that):
|
85
|
+
raise RequirementNotFulfilledError(that, when, subwhen) from None
|
86
|
+
except:
|
87
|
+
raise RequirementNotFulfilledError(that, when, subwhen) from None
|
@@ -75,14 +75,14 @@ class TestCase_requiresthat_01:
|
|
75
75
|
|
76
76
|
class Spam:
|
77
77
|
|
78
|
-
@requires(that='self.spam == "
|
78
|
+
@requires(that='self.spam == "ham"')
|
79
79
|
def __init__(self):
|
80
80
|
self.spam = 'ham'
|
81
81
|
|
82
82
|
def run(self): ...
|
83
83
|
|
84
|
-
# We break the constructor
|
85
|
-
with pytest.raises(
|
84
|
+
# We break the constructor
|
85
|
+
with pytest.raises(RequirementNotFulfilledError):
|
86
86
|
S = Spam()
|
87
87
|
|
88
88
|
# Remember, the constructor just failed...
|
@@ -1,62 +0,0 @@
|
|
1
|
-
"""See the `README
|
2
|
-
<https://gitlab.com/bedhanger/mwe/-/blob/master/python/requiresthat/README.rst>`_ file
|
3
|
-
"""
|
4
|
-
from typing import Optional, Callable
|
5
|
-
from functools import wraps
|
6
|
-
|
7
|
-
from ._when import When, APRIORI, POSTMORTEM, BEFOREANDAFTER
|
8
|
-
from ._exceptions import RequirementNotFulfilledError, NoCallableConstructError
|
9
|
-
|
10
|
-
def requires(that, when: When = APRIORI) -> Optional[Callable]:
|
11
|
-
"""Require <that> of the decoratee, and require it <when>"""
|
12
|
-
|
13
|
-
def func_wrapper(func: Callable) -> Optional[Callable]:
|
14
|
-
"""First-level wrap the decoratee"""
|
15
|
-
|
16
|
-
@wraps(func)
|
17
|
-
def inner_wrapper(self, *pargs, **kwargs) -> Optional[Callable]:
|
18
|
-
"""Wrap the first-level wrapper
|
19
|
-
|
20
|
-
The wrapping stops here...
|
21
|
-
"""
|
22
|
-
try:
|
23
|
-
assert callable(func)
|
24
|
-
except AssertionError as exc:
|
25
|
-
raise NoCallableConstructError(func) from exc
|
26
|
-
else:
|
27
|
-
# Since we want to give detailed sub-failure diax in case of BEFOREANDAFTER,
|
28
|
-
# economisng on the ifs below is tricky.
|
29
|
-
if when == APRIORI:
|
30
|
-
try:
|
31
|
-
assert eval(that)
|
32
|
-
except AssertionError as exc:
|
33
|
-
raise RequirementNotFulfilledError(that, when) from exc
|
34
|
-
else:
|
35
|
-
func(self, *pargs, **kwargs)
|
36
|
-
|
37
|
-
elif when == POSTMORTEM:
|
38
|
-
func(self, *pargs, **kwargs)
|
39
|
-
try:
|
40
|
-
assert eval(that)
|
41
|
-
except AssertionError as exc:
|
42
|
-
raise RequirementNotFulfilledError(that, when) from exc
|
43
|
-
|
44
|
-
elif when == BEFOREANDAFTER:
|
45
|
-
try:
|
46
|
-
assert eval(that)
|
47
|
-
except AssertionError as exc:
|
48
|
-
raise RequirementNotFulfilledError(that, when, APRIORI) from exc
|
49
|
-
else:
|
50
|
-
func(self, *pargs, **kwargs)
|
51
|
-
try:
|
52
|
-
assert eval(that)
|
53
|
-
except AssertionError as exc:
|
54
|
-
raise RequirementNotFulfilledError(that, when, POSTMORTEM) from exc
|
55
|
-
|
56
|
-
# We don't need an else clause; trying to enlist something that's not in the enum
|
57
|
-
# will be penalised with an AttributeError, and small typos will be healed with a
|
58
|
-
# suggestion as to what you might have meant.
|
59
|
-
|
60
|
-
return inner_wrapper
|
61
|
-
|
62
|
-
return func_wrapper
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat.egg-info/dependency_links.txt
RENAMED
File without changes
|
{requiresthat-2025.6.19.3 → requiresthat-2025.6.20.0}/src/requiresthat.egg-info/top_level.txt
RENAMED
File without changes
|