From d9bcf7ebb3096e7ebf8ef8c6a8588ed200d801e0 Mon Sep 17 00:00:00 2001 From: Sietze van Buuren Date: Sun, 18 Aug 2024 12:38:47 +0200 Subject: [PATCH 1/2] test: Add tests for decorator keyword arguments usage Signed-off-by: Sietze van Buuren --- pqthreads/examples/worker.py | 20 ++++++++++++++++---- tests/basic_test.py | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) 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' From 499031937a2923ca5b3099ba9f2f5587c4d8b0a4 Mon Sep 17 00:00:00 2001 From: Sietze van Buuren Date: Sun, 18 Aug 2024 12:39:34 +0200 Subject: [PATCH 2/2] fix: Decorator keyword arguments input Signed-off-by: Sietze van Buuren --- pqthreads/decorator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)