Skip to content
Snippets Groups Projects
Commit b9c09806 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

fix execute_before

parent 1a1d9ac2
No related branches found
No related tags found
No related merge requests found
__version__ = '2.9.12_a2'
__version__ = '2.9.12_a3'
......@@ -28,23 +28,34 @@ def execute_before(callable_: tp.Callable[[], None]):
>>> ...
>>> nothing()
As well as the following:
You can even specify custom parameters for the callable:
>>> do_things(execute=True)
to simply execute the callable.
>>> @execute_before
>>> def i_am_2(two):
>>> assert two == 2
>>> @i_am_2(2)
>>> def run_me():
>>> pass
"""
def outer(fun=None, execute: bool = False):
if execute:
return callable_()
else:
def outer(*args, **kwargs):
if len(args) == 1 and not kwargs and callable(args[0]):
fun = args[0]
@wraps(fun)
def inner(*args, **kwargs):
def inner(*my_args, **my_kwargs):
callable_()
return fun(*args, **kwargs)
return fun(*my_args, **my_kwargs)
return inner
else:
def inner(func):
@wraps(func)
def inner2(*my_args, **my_kwargs):
callable_(*args, **kwargs)
return func(*my_args, **my_kwargs)
return inner2
return inner
return outer
......
......@@ -18,17 +18,17 @@ class TestDecorators(unittest.TestCase):
a = 0
@execute_before
def increase_a():
def increase_a(factor=1):
nonlocal a
a += 1
a += factor
@increase_a
@increase_a(factor=2)
def launch_me():
nonlocal a
a += 1
launch_me()
self.assertEqual(a, 2)
self.assertEqual(a, 3)
def test_precondition_none(self):
@precondition(short_none('x == 2'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment