From b3e87b9f188b5dcaba081a1c0cc5310f6b9ad6a3 Mon Sep 17 00:00:00 2001 From: Garulf <535299+Garulf@users.noreply.github.com> Date: Tue, 13 Feb 2024 20:34:51 -0500 Subject: [PATCH 1/3] Add testing for Python 3.12 --- .github/workflows/tests.yaml | 2 +- tox.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index edee58f..e2a42ed 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -19,7 +19,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.8', '3.9', '3.10', '3.11'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v3 diff --git a/tox.ini b/tox.ini index 8c3fc59..3368106 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] package_name = "pyflowlauncher" -envlist = lint, type, py{38,39,310,311} +envlist = lint, type, py{38,39,310,311,312} [testenv] deps = pytest From 4bf2a996eec7337fbc0272b0a405d41cfb4081e3 Mon Sep 17 00:00:00 2001 From: Garulf <535299+Garulf@users.noreply.github.com> Date: Tue, 13 Feb 2024 20:35:23 -0500 Subject: [PATCH 2/3] Add async maybe logic --- pyflowlauncher/event.py | 10 ++++++---- pyflowlauncher/plugin.py | 7 ++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pyflowlauncher/event.py b/pyflowlauncher/event.py index 9919d52..c156323 100644 --- a/pyflowlauncher/event.py +++ b/pyflowlauncher/event.py @@ -1,5 +1,4 @@ - - +import asyncio from typing import Any, Callable, Iterable, Type @@ -24,9 +23,12 @@ def add_methods(self, methods: Iterable[Callable[..., Any]]): def add_exception_handler(self, exception: Type[Exception], handler: Callable[..., Any]): self._handlers[exception] = handler - def __call__(self, method: str, *args, **kwargs): + async def __call__(self, method: str, *args, **kwargs): try: - return self._methods[method](*args, **kwargs) + result = self._methods[method](*args, **kwargs) + if asyncio.iscoroutine(result): + return await result + return result except Exception as e: handler = self._handlers.get(type(e), None) if handler: diff --git a/pyflowlauncher/plugin.py b/pyflowlauncher/plugin.py index b4431a0..dca701e 100644 --- a/pyflowlauncher/plugin.py +++ b/pyflowlauncher/plugin.py @@ -5,6 +5,7 @@ from typing import Any, Callable, Iterable, Optional, Type, Union from pathlib import Path import json +import asyncio from pyflowlauncher.shared import logger @@ -71,7 +72,11 @@ def run(self) -> None: request = self._client.recieve() method = request["method"] parameters = request.get('parameters', []) - feedback = self._event_handler(method, *parameters) + if sys.version_info >= (3, 10, 0): + feedback = asyncio.run(self._event_handler(method, *parameters)) + else: + loop = asyncio.get_event_loop() + feedback = loop.run_until_complete(self._event_handler(method, *parameters)) if not feedback: return self._client.send(feedback) From 7115f71694a25d8fabef0d5f2164a48d8bb931f3 Mon Sep 17 00:00:00 2001 From: Garulf <535299+Garulf@users.noreply.github.com> Date: Tue, 13 Feb 2024 20:35:31 -0500 Subject: [PATCH 3/3] Add testing for async --- requirements-dev.txt | 1 + tests/test_events.py | 21 +++++++++++++++++---- tox.ini | 4 +++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 2d1cdac..369fb58 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,6 @@ flake8 pytest +pytest-asyncio tox mypy>=1.7.0 bump-my-version>=0.12.0 \ No newline at end of file diff --git a/tests/test_events.py b/tests/test_events.py index d301cd5..cf96923 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -10,6 +10,10 @@ def temp_method2(): return None +async def async_temp_method3(): + return None + + def except_method(): raise Exception @@ -26,10 +30,18 @@ def test_add_methods(): assert handler._methods == {"temp_method1": temp_method1, "temp_method2": temp_method2} -def test_call(): +@pytest.mark.asyncio +async def test_call(): handler = EventHandler() handler.add_method(temp_method1) - assert handler("temp_method1") is None + assert await handler("temp_method1") is None + + +@pytest.mark.asyncio +async def test_call_async(): + handler = EventHandler() + handler.add_method(async_temp_method3) + assert await handler("async_temp_method3") is None def test_add_exception_handler(): @@ -38,8 +50,9 @@ def test_add_exception_handler(): assert handler._handlers == {Exception: temp_method1} -def test_call_exception(): +@pytest.mark.asyncio +async def test_call_exception(): handler = EventHandler() handler.add_method(except_method) with pytest.raises(Exception): - handler("except_method") + await handler("except_method") diff --git a/tox.ini b/tox.ini index 3368106..fa4868e 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,9 @@ package_name = "pyflowlauncher" envlist = lint, type, py{38,39,310,311,312} [testenv] -deps = pytest +deps = + pytest + pytest-asyncio commands = pytest {posargs:tests}