mirror of
https://github.com/NCBM/plyngent.git
synced 2026-07-25 08:04:57 +08:00
test/tools: catalog, grants, and async tool helpers
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import inspect
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
@@ -8,8 +9,20 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
def call_sync(definition: ToolDefinition, *args: object, **kwargs: object) -> str:
|
||||
"""Invoke a tool handler from a **sync** test.
|
||||
|
||||
Async handlers are run with ``asyncio.run``. Inside an already-running
|
||||
event loop (async tests), use :func:`call_async` instead.
|
||||
"""
|
||||
result: Any = definition.handler(*args, **kwargs)
|
||||
assert not inspect.isawaitable(result)
|
||||
if inspect.isawaitable(result):
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
result = asyncio.run(result)
|
||||
else:
|
||||
msg = "call_sync cannot await inside a running event loop; use call_async"
|
||||
raise RuntimeError(msg)
|
||||
return cast("str", result)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user