Skip to content

Commit

Permalink
Merge pull request #5 from Garulf/async-maybe
Browse files Browse the repository at this point in the history
Async maybe
  • Loading branch information
Garulf authored Feb 15, 2024
2 parents 5a3dd9e + 7115f71 commit ef25cd6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pyflowlauncher/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


import asyncio
from typing import Any, Callable, Iterable, Type


Expand All @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion pyflowlauncher/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
flake8
pytest
pytest-asyncio
tox
mypy>=1.7.0
bump-my-version>=0.12.0
21 changes: 17 additions & 4 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def temp_method2():
return None


async def async_temp_method3():
return None


def except_method():
raise Exception

Expand All @@ -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():
Expand All @@ -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")
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[tox]
package_name = "pyflowlauncher"
envlist = lint, type, py{38,39,310,311}
envlist = lint, type, py{38,39,310,311,312}

[testenv]
deps = pytest
deps =
pytest
pytest-asyncio
commands =
pytest {posargs:tests}

Expand Down

0 comments on commit ef25cd6

Please sign in to comment.