diff --git a/pqthreads/decorator.py b/pqthreads/decorator.py index 8babfee..913155d 100644 --- a/pqthreads/decorator.py +++ b/pqthreads/decorator.py @@ -55,7 +55,7 @@ class Decorator: # pylint: disable=too-few-public-methods def __init__(self, decorator_class: DecoratorCore): self.decorator_class = decorator_class - def __call__(self, wrapped, **kwargs): + def __call__(self, wrapped=None, **kwargs): """ Enables usage with and without decorator keyword arguments """ if wrapped is None: return functools.partial(self.__call__, **kwargs) diff --git a/pqthreads/examples/worker.py b/pqthreads/examples/worker.py index eeff047..f7f93a1 100644 --- a/pqthreads/examples/worker.py +++ b/pqthreads/examples/worker.py @@ -50,8 +50,20 @@ def graph(*args, **kwargs): return graph_worker +global_kwargs = {} + + # Configure decorators -DecoratorCore = decorator.DecoratorCore -DecoratorCore.add_agent('figure', window.FigureWindow, FigureWorker) -DecoratorCore.add_agent('graph', window.GraphWindow, GraphWorker) -decorator_example = decorator.Decorator(DecoratorCore) +class ExampleDecoratorCore(decorator.DecoratorCore): + """ Exemplary decorator core class, with extra testing functionality for + global keyword arguments """ + + def __init__(self, **dec_kwargs): + """ Reimplement this to make use of decorator's keyword arguments """ + global_kwargs.update(dec_kwargs) + super().__init__(**dec_kwargs) + + +ExampleDecoratorCore.add_agent('figure', window.FigureWindow, FigureWorker) +ExampleDecoratorCore.add_agent('graph', window.GraphWindow, GraphWorker) +decorator_example = decorator.Decorator(ExampleDecoratorCore) diff --git a/tests/basic_test.py b/tests/basic_test.py index e580c12..1ad8e46 100644 --- a/tests/basic_test.py +++ b/tests/basic_test.py @@ -121,3 +121,30 @@ def main(): result = main() assert result == 'Test successful' + + +def test_decorator_keyword_argument(): + """ Test functionality of single decorator keyword argument """ + + @worker.decorator_example(keyword_argument='test') + def main(): + """ Helper function """ + fig = worker.figure() + fig.close() + + main() + assert worker.global_kwargs['keyword_argument'] == 'test' + + +def test_multiple_decorator_keyword_arguments(): + """ Test functionality of multiple decorator keyword arguments """ + + @worker.decorator_example(first_kwarg='first_arg', second_kw='second_arg') + def main(): + """ Helper function """ + fig = worker.figure() + fig.close() + + main() + assert worker.global_kwargs['first_kwarg'] == 'first_arg' + assert worker.global_kwargs['second_kw'] == 'second_arg'