From 8da218116c237d1f8dfa474160f35f172b02d0c5 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 24 Jul 2026 16:24:32 +0200 Subject: [PATCH 1/2] feat(async): greenlet Submitter and PluginManager.run_async Complete design step 07: - New _async module with a persistent Submitter: maybe_submit awaits awaitable hook results while active and passes them through otherwise (await-me-maybe); require_await hard-fails outside async context; async_generator_to_sync helps wrappers consume async generators. - Submitter.run uses a sentinel so legitimate None returns work (fixes the try-claude footgun) and forwards await failures to the submission site inside the worker greenlet. - PluginManager owns the Submitter and threads it through the callers and _hookexec into _multicall - activation is purely Submitter.run, no _inner_hookexec monkeypatching. await pm.run_async(func) is the public entry point; nested runs raise. - Packaging: new pluggy[async] extra depending on greenlet; greenlet added to the testing group and types-greenlet to the mypy hook. Co-Authored-By: Claude Fable 5 --- .pre-commit-config.yaml | 2 +- changelog/710.feature.rst | 9 ++ pyproject.toml | 5 +- src/pluggy/_async.py | 163 +++++++++++++++++++++ src/pluggy/_caller.py | 40 +++++- src/pluggy/_execution.py | 10 ++ src/pluggy/_manager.py | 73 +++++++++- testing/benchmark.py | 12 +- testing/test_async.py | 288 ++++++++++++++++++++++++++++++++++++++ testing/test_multicall.py | 5 +- uv.lock | 95 +++++++++++++ 11 files changed, 686 insertions(+), 16 deletions(-) create mode 100644 changelog/710.feature.rst create mode 100644 src/pluggy/_async.py create mode 100644 testing/test_async.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6af69994..c66a3b2c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -46,4 +46,4 @@ repos: - id: mypy files: ^(src/|testing/) args: [] - additional_dependencies: [pytest] + additional_dependencies: [pytest, types-greenlet] diff --git a/changelog/710.feature.rst b/changelog/710.feature.rst new file mode 100644 index 00000000..7f0f351f --- /dev/null +++ b/changelog/710.feature.rst @@ -0,0 +1,9 @@ +New async bridge for awaitable hook results: +``await pm.run_async(lambda: pm.hook.my_hook())`` runs the hook call in a +greenlet context where awaitable results from hook implementations are +awaited on the current event loop ("await-me-maybe"). Outside +``run_async``, awaitable results are passed through unchanged, as before. +Requires the optional ``greenlet`` dependency, installable via +``pluggy[async]``. The bridge is a persistent per-manager ``Submitter``; +async wrapper generators are not auto-awaited (a manual +``async_generator_to_sync`` helper is provided). diff --git a/pyproject.toml b/pyproject.toml index beacdda6..20a58126 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,9 +44,12 @@ Repository = "https://github.com/pytest-dev/pluggy" Issues = "https://github.com/pytest-dev/pluggy/issues" Funding = "https://tidelift.com/subscription/pkg/pypi-pluggy" +[project.optional-dependencies] +async = ["greenlet"] + [dependency-groups] dev = ["pre-commit"] -testing = ["pytest>=8", "pytest-benchmark", "coverage"] +testing = ["pytest>=8", "pytest-benchmark", "coverage", "greenlet"] [tool.setuptools] diff --git a/src/pluggy/_async.py b/src/pluggy/_async.py new file mode 100644 index 00000000..e5a00970 --- /dev/null +++ b/src/pluggy/_async.py @@ -0,0 +1,163 @@ +""" +Async support for pluggy using greenlets. + +This module provides async functionality for pluggy, allowing hook +implementations to return awaitable objects that are automatically awaited +when running in an async context (see :meth:`PluginManager.run_async +`). +""" + +from __future__ import annotations + +from collections.abc import AsyncGenerator +from collections.abc import Awaitable +from collections.abc import Callable +from collections.abc import Generator +from typing import Any +from typing import cast +from typing import Final +from typing import TYPE_CHECKING +from typing import TypeVar + + +_T = TypeVar("_T") +_Y = TypeVar("_Y") +_S = TypeVar("_S") + +if TYPE_CHECKING: + import greenlet + + +#: Sentinel distinguishing "worker never completed" from a legitimate +#: ``None`` result. +_UNSET: Final = object() + + +class Submitter: + """Bridge between synchronous hook execution and an async event loop. + + While :meth:`run` is active, awaitables passed to :meth:`maybe_submit` or + :meth:`require_await` are switched to the awaiting parent greenlet, which + awaits them and switches the result back. When inactive, + :meth:`maybe_submit` passes awaitables through unchanged + ("await-me-maybe"). + """ + + _active_submitter: greenlet.greenlet | None + + def __init__(self) -> None: + self._active_submitter = None + + def __repr__(self) -> str: + return f"" + + def maybe_submit(self, coro: Awaitable[_T]) -> _T | Awaitable[_T]: + """Await an awaitable if active, else return it unchanged. + + This enables backward compatibility for datasette + and https://simonwillison.net/2020/Sep/2/await-me-maybe/ + """ + active = self._active_submitter + if active is not None: + # We're in a greenlet context; switch to the parent with the + # awaitable. The parent awaits it and switches back the result. + res: _T = active.switch(coro) + return res + else: + return coro + + def require_await(self, coro: Awaitable[_T]) -> _T: + """Await an awaitable, raising an error if not in async context.""" + active = self._active_submitter + if active is not None: + res: _T = active.switch(coro) + return res + else: + raise RuntimeError("require_await called outside of async context") + + async def run(self, sync_func: Callable[[], _T]) -> _T: + """Run a synchronous function in a worker greenlet with async support. + + Awaitables submitted by hook implementations during the call are + awaited on this coroutine's event loop. + """ + try: + import greenlet + except ImportError: + raise RuntimeError("greenlet is required for async support") from None + + if self._active_submitter is not None: + raise RuntimeError("Submitter is already active") + + main_greenlet = greenlet.getcurrent() + result: object = _UNSET + exception: BaseException | None = None + + def greenlet_func() -> None: + nonlocal result, exception + try: + result = sync_func() + except BaseException as e: + exception = e + + worker_greenlet = greenlet.greenlet(greenlet_func) + # Let maybe_submit/require_await switch back to this greenlet. + self._active_submitter = main_greenlet + try: + # Run the worker; every switch back carries an awaitable to + # process, until the worker finishes (switching back None). + awaitable = worker_greenlet.switch() + while awaitable is not None: + try: + awaited_result = await awaitable + except BaseException as e: + # Raise at the submission site inside the worker. + awaitable = worker_greenlet.throw(e) + else: + awaitable = worker_greenlet.switch(awaited_result) + finally: + self._active_submitter = None + + if exception is not None: + raise exception + assert result is not _UNSET, "worker greenlet did not complete" + return cast(_T, result) + + +def async_generator_to_sync( + async_gen: AsyncGenerator[_Y, _S], submitter: Submitter +) -> Generator[_Y, _S, None]: + """Convert an async generator to a sync generator using a `Submitter`. + + This helper allows wrapper implementations to use async generators while + maintaining compatibility with the sync generator interface expected by + the hook system. The submitter must be active (i.e. the generator must be + consumed under :meth:`Submitter.run`). + """ + try: + # Start the async generator. + value = submitter.require_await(async_gen.__anext__()) + + while True: + try: + sent_value = yield value + try: + value = submitter.require_await(async_gen.asend(sent_value)) + except StopAsyncIteration: + return + + except GeneratorExit: + # Generator is being closed; close the async generator too. + submitter.require_await(cast(Awaitable[Any], async_gen.aclose())) + raise + + except BaseException as exc: + # Exception was thrown into the generator; forward it into + # the async generator. + try: + value = submitter.require_await(async_gen.athrow(exc)) + except StopAsyncIteration: + return + + except StopAsyncIteration: + return diff --git a/src/pluggy/_caller.py b/src/pluggy/_caller.py index edb203e1..cda3df0a 100644 --- a/src/pluggy/_caller.py +++ b/src/pluggy/_caller.py @@ -19,6 +19,7 @@ from typing import TypeAlias from typing import TypeVar +from ._async import Submitter from ._config import HookimplConfiguration from ._config import hookspec_config_from_mapping from ._config import HookspecConfiguration @@ -31,7 +32,14 @@ _HookExec: TypeAlias = Callable[ - [str, Sequence[NormalImpl], Sequence[WrapperImpl], Mapping[str, object], bool], + [ + str, + Sequence[NormalImpl], + Sequence[WrapperImpl], + Mapping[str, object], + bool, + Submitter, + ], "object | list[object]", ] @@ -153,6 +161,7 @@ class NormalHookCaller: """A caller of all registered implementations of a hook specification.""" __slots__ = ( + "_async_submitter", "_hookexec", "_normal_hookimpls", "_wrapper_hookimpls", @@ -166,11 +175,13 @@ def __init__( hook_execute: _HookExec, specmodule_or_class: _Namespace | None = None, spec_config: HookspecConfiguration | None = None, + async_submitter: Submitter | None = None, ) -> None: """:meta private:""" #: Name of the hook getting called. self.name: Final = name self._hookexec: Final = hook_execute + self._async_submitter: Final = async_submitter or Submitter() # Split hook implementations into two lists for simpler management: # Normal hooks: [trylast, normal, tryfirst] # Wrapper hooks: [trylast, normal, tryfirst] @@ -262,6 +273,7 @@ def __call__(self, **kwargs: object) -> Any: self._wrapper_hookimpls.copy(), call_kwargs, firstresult, + self._async_submitter, ) def call_historic( @@ -297,6 +309,7 @@ def call_extra( self._wrapper_hookimpls.copy(), kwargs, firstresult, + self._async_submitter, ) def _maybe_apply_history(self, method: HookImpl) -> None: @@ -316,6 +329,7 @@ class HistoricHookCaller: """ __slots__ = ( + "_async_submitter", "_call_history", "_hookexec", "_hookimpls", @@ -331,12 +345,14 @@ def __init__( hook_execute: _HookExec, specmodule_or_class: _Namespace, spec_config: HookspecConfiguration, + async_submitter: Submitter | None = None, ) -> None: """:meta private:""" assert spec_config.historic, "HistoricHookCaller requires historic=True" #: Name of the hook getting called. self.name: Final = name self._hookexec: Final = hook_execute + self._async_submitter: Final = async_submitter or Submitter() # The hookimpls list for historic hooks (no wrappers supported). self._hookimpls: Final[list[NormalImpl]] = [] self._call_history: Final[_CallHistory] = [] @@ -410,7 +426,14 @@ def call_historic( # Historizing hooks don't return results. # Remember firstresult isn't compatible with historic. # Copy because plugins may register other plugins during iteration (#438). - res = self._hookexec(self.name, self._hookimpls.copy(), [], kwargs, False) + res = self._hookexec( + self.name, + self._hookimpls.copy(), + [], + kwargs, + False, + self._async_submitter, + ) if result_callback is None: return if isinstance(res, list): @@ -429,7 +452,9 @@ def _maybe_apply_history(self, method: HookImpl) -> None: """Apply call history to a new hookimpl.""" assert isinstance(method, NormalImpl) for kwargs, result_callback in self._call_history: - res = self._hookexec(self.name, [method], [], kwargs, False) + res = self._hookexec( + self.name, [method], [], kwargs, False, self._async_submitter + ) if res and result_callback is not None: # XXX: remember firstresult isn't compat with historic assert isinstance(res, list) @@ -511,6 +536,7 @@ def __call__(self, **kwargs: object) -> Any: self._get_filtered(orig._wrapper_hookimpls), call_kwargs, firstresult, + orig._async_submitter, ) def call_historic( @@ -532,7 +558,12 @@ def call_historic( # History is shared with the original caller. orig._call_history.append((kwargs, result_callback)) res = orig._hookexec( - self.name, self._get_filtered(orig._hookimpls), [], kwargs, False + self.name, + self._get_filtered(orig._hookimpls), + [], + kwargs, + False, + orig._async_submitter, ) if result_callback is None: return @@ -568,6 +599,7 @@ def call_extra( self._get_filtered(orig._wrapper_hookimpls), kwargs, firstresult, + orig._async_submitter, ) def __repr__(self) -> str: diff --git a/src/pluggy/_execution.py b/src/pluggy/_execution.py index ef7fd3a4..5a90e2bb 100644 --- a/src/pluggy/_execution.py +++ b/src/pluggy/_execution.py @@ -4,6 +4,7 @@ from __future__ import annotations +from collections.abc import Awaitable from collections.abc import Generator from collections.abc import Mapping from collections.abc import Sequence @@ -20,6 +21,10 @@ from ._warnings import PluggyTeardownRaisedWarning +if TYPE_CHECKING: + from ._async import Submitter + + # Need to distinguish between old- and new-style hook wrappers. # Wrapping with a tuple is the fastest type-safe way I found to do it. Teardown: TypeAlias = Generator[None, object, object] @@ -93,6 +98,7 @@ def _multicall( wrapper_impls: Sequence[WrapperImpl], caller_kwargs: Mapping[str, object], firstresult: bool, + async_submitter: Submitter, ) -> object | list[object]: """Execute a call into multiple python functions/methods and return the result(s). @@ -127,6 +133,10 @@ def _multicall( else: res = normal_impl.function(*args, **kwargs) if res is not None: + # Awaitable results are awaited when a Submitter is active + # (await-me-maybe), otherwise passed through unchanged. + if isinstance(res, Awaitable): + res = async_submitter.maybe_submit(res) results.append(res) if firstresult: # halt further impl calls break diff --git a/src/pluggy/_manager.py b/src/pluggy/_manager.py index 1f523b11..a9de7f87 100644 --- a/src/pluggy/_manager.py +++ b/src/pluggy/_manager.py @@ -11,9 +11,11 @@ from typing import Final from typing import TYPE_CHECKING from typing import TypeAlias +from typing import TypeVar import warnings from . import _tracing +from ._async import Submitter from ._callers import _multicall from ._compat import DistFacade from ._config import hookimpl_config_from_mapping @@ -43,6 +45,8 @@ import importlib.metadata +_T = TypeVar("_T") + _BeforeTrace: TypeAlias = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None] _AfterTrace: TypeAlias = Callable[ [Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None @@ -167,15 +171,24 @@ class PluginManager: :param project_name: The short project name (prefer snake case, make sure it's unique!), or a :class:`ProjectSpec` instance. + :param async_submitter: + Custom :class:`~pluggy._async.Submitter` used to await awaitable hook + results under :meth:`run_async` (defaults to a fresh one). .. versionchanged:: 1.7 A :class:`ProjectSpec` may be passed instead of a plain name. + Added ``async_submitter`` and :meth:`run_async`. """ - def __init__(self, project_name: str | ProjectSpec) -> None: + def __init__( + self, + project_name: str | ProjectSpec, + async_submitter: Submitter | None = None, + ) -> None: self._project_spec: Final = ( ProjectSpec(project_name) if isinstance(project_name, str) else project_name ) + self._async_submitter: Final = async_submitter or Submitter() self._name2plugin: Final[dict[str, _Plugin]] = {} self._plugin_distinfo: Final[ list[tuple[_Plugin, importlib.metadata.Distribution]] @@ -201,11 +214,17 @@ def _hookexec( wrapper_impls: Sequence[WrapperImpl], caller_kwargs: Mapping[str, object], firstresult: bool, + async_submitter: Submitter, ) -> object | list[object]: # called from all hookcaller instances. # enable_tracing will set its own wrapping function at self._inner_hookexec return self._inner_hookexec( - hook_name, normal_impls, wrapper_impls, caller_kwargs, firstresult + hook_name, + normal_impls, + wrapper_impls, + caller_kwargs, + firstresult, + async_submitter, ) def register(self, plugin: _Plugin, name: str | None = None) -> str | None: @@ -273,7 +292,11 @@ def register(self, plugin: _Plugin, name: str | None = None) -> str | None: for hook_name, hookimpl, verified in hookimpls: hook = getattr(self.hook, hook_name, None) if hook is None: - hook = NormalHookCaller(hook_name, self._hookexec) + hook = NormalHookCaller( + hook_name, + self._hookexec, + async_submitter=self._async_submitter, + ) setattr(self.hook, hook_name, hook) elif not verified and hook.has_spec(): # An earlier impl's historic replay added this spec after the @@ -417,11 +440,19 @@ def add_hookspecs(self, module_or_class: _Namespace) -> None: if hc is None: if spec_config.historic: hc = HistoricHookCaller( - name, self._hookexec, module_or_class, spec_config + name, + self._hookexec, + module_or_class, + spec_config, + async_submitter=self._async_submitter, ) else: hc = NormalHookCaller( - name, self._hookexec, module_or_class, spec_config + name, + self._hookexec, + module_or_class, + spec_config, + async_submitter=self._async_submitter, ) setattr(self.hook, name, hc) elif spec_config.historic and not hc.is_historic(): @@ -435,7 +466,11 @@ def add_hookspecs(self, module_or_class: _Namespace) -> None: raise AssertionError("unreachable") # pragma: no cover old_hookimpls = hc.get_hookimpls() historic_hc = HistoricHookCaller( - name, self._hookexec, module_or_class, spec_config + name, + self._hookexec, + module_or_class, + spec_config, + async_submitter=self._async_submitter, ) setattr(self.hook, name, historic_hc) for hookimpl in old_hookimpls: @@ -705,6 +740,7 @@ def traced_hookexec( wrapper_impls: Sequence[WrapperImpl], caller_kwargs: Mapping[str, object], firstresult: bool, + async_submitter: Submitter, ) -> object | list[object]: # For backward compatibility of the before/after callback shapes, # combine the split lists into one. @@ -712,7 +748,12 @@ def traced_hookexec( before(hook_name, hook_impls, caller_kwargs) outcome = Result.from_call( lambda: oldcall( - hook_name, normal_impls, wrapper_impls, caller_kwargs, firstresult + hook_name, + normal_impls, + wrapper_impls, + caller_kwargs, + firstresult, + async_submitter, ) ) after(outcome, hook_name, hook_impls, caller_kwargs) @@ -750,6 +791,24 @@ def after( return self.add_hookcall_monitoring(before, after) + async def run_async(self, func: Callable[[], _T]) -> _T: + """Run a synchronous function with async support for hook results. + + The function runs in a greenlet context in which awaitable hook + results are automatically awaited on the current event loop:: + + pm = PluginManager("myapp") + ... + results = await pm.run_async(lambda: pm.hook.my_hook()) + + :raises RuntimeError: + If ``greenlet`` is not installed (install ``pluggy[async]``), or + if a ``run_async`` call is already active for this manager. + + .. versionadded:: 1.7 + """ + return await self._async_submitter.run(func) + def subset_hook_caller( self, name: str, remove_plugins: Iterable[_Plugin] ) -> HookCaller: diff --git a/testing/benchmark.py b/testing/benchmark.py index 6d021d17..cc40e8a8 100644 --- a/testing/benchmark.py +++ b/testing/benchmark.py @@ -12,6 +12,7 @@ from pluggy import HookspecMarker from pluggy import PluginManager from pluggy import WrapperImpl +from pluggy._async import Submitter from pluggy._callers import _multicall from pluggy._hooks import varnames @@ -119,7 +120,14 @@ def setup(): normal_impls.append(f) caller_kwargs = {"arg1": 1, "arg2": 2, "arg3": 3} firstresult = False - return (hook_name, normal_impls, wrapper_impls, caller_kwargs, firstresult), {} + return ( + hook_name, + normal_impls, + wrapper_impls, + caller_kwargs, + firstresult, + Submitter(), + ), {} benchmark.pedantic(_multicall, setup=setup, rounds=100) @@ -140,7 +148,7 @@ def setup(): caller_kwargs = {"arg1": 1} if pass_arg: caller_kwargs["arg2"] = 2 - return ("foo", normal_impls, [], caller_kwargs, False), {} + return ("foo", normal_impls, [], caller_kwargs, False, Submitter()), {} benchmark.pedantic(_multicall, setup=setup, rounds=100) diff --git a/testing/test_async.py b/testing/test_async.py new file mode 100644 index 00000000..2b8c790e --- /dev/null +++ b/testing/test_async.py @@ -0,0 +1,288 @@ +""" +Tests for the greenlet-based async Submitter support. +""" + +from __future__ import annotations + +import asyncio +from collections.abc import AsyncGenerator +from collections.abc import Awaitable +from collections.abc import Coroutine +from typing import Any +from typing import cast + +import pytest + +import pluggy +from pluggy._async import async_generator_to_sync +from pluggy._async import Submitter + + +hookspec = pluggy.HookspecMarker("test") +hookimpl = pluggy.HookimplMarker("test") + + +class HookSpecs: + @hookspec + def test_hook(self): + pass + + +class AsyncPlugin: + @hookimpl + async def test_hook(self): + await asyncio.sleep(0.01) + return "async_result" + + +class SyncPlugin: + @hookimpl + def test_hook(self): + return "sync_result" + + +def make_pm(*plugins: object) -> pluggy.PluginManager: + pm = pluggy.PluginManager("test") + pm.add_hookspecs(HookSpecs) + for plugin in plugins: + pm.register(plugin) + return pm + + +def test_run_async_mixed_sync_and_async() -> None: + pytest.importorskip("greenlet") + pm = make_pm(AsyncPlugin(), SyncPlugin()) + + async def run_test() -> list[str]: + result = await pm.run_async(lambda: pm.hook.test_hook()) + return cast(list[str], result) + + result = asyncio.run(run_test()) + assert "sync_result" in result + assert "async_result" in result + + +def test_run_async_with_sync_hooks_only() -> None: + pytest.importorskip("greenlet") + pm = make_pm(SyncPlugin()) + + async def run_test() -> list[str]: + result = await pm.run_async(lambda: pm.hook.test_hook()) + return cast(list[str], result) + + assert asyncio.run(run_test()) == ["sync_result"] + + +def test_awaitable_left_in_results_outside_run_async() -> None: + """Outside run_async, awaitables are passed through (await-me-maybe).""" + pm = make_pm(AsyncPlugin(), SyncPlugin()) + + results = pm.hook.test_hook() + assert "sync_result" in results + awaitables = [res for res in results if isinstance(res, Awaitable)] + assert len(awaitables) == 1 + + # Awaiting the leftover coroutine still works. + leftover = cast("Coroutine[Any, Any, str]", awaitables[0]) + assert asyncio.run(leftover) == "async_result" + + +def test_run_async_returning_none_succeeds() -> None: + """Regression test: a legitimate None result must not be rejected.""" + pytest.importorskip("greenlet") + pm = make_pm() + + async def run_test() -> None: + return await pm.run_async(lambda: None) + + assert asyncio.run(run_test()) is None + + +def test_nested_run_async_rejected() -> None: + pytest.importorskip("greenlet") + pm = make_pm() + inner_error: list[BaseException] = [] + + def nested() -> object: + submitter = pm._async_submitter + # Submitter is already active - a nested run must hard-fail. + coro = submitter.run(lambda: 1) + try: + coro.send(None) + except RuntimeError as e: + inner_error.append(e) + finally: + coro.close() + return "done" + + async def run_test() -> object: + return await pm.run_async(nested) + + assert asyncio.run(run_test()) == "done" + assert inner_error + assert "already active" in str(inner_error[0]) + + +def test_run_async_propagates_sync_exception() -> None: + pytest.importorskip("greenlet") + pm = make_pm() + + def boom() -> None: + raise ValueError("boom") + + async def run_test() -> None: + await pm.run_async(boom) + + with pytest.raises(ValueError, match="boom"): + asyncio.run(run_test()) + + +def test_run_async_propagates_awaited_exception() -> None: + """An exception raised while awaiting surfaces at the hook call site.""" + pytest.importorskip("greenlet") + + class FailingAsyncPlugin: + @hookimpl + async def test_hook(self): + raise ValueError("async boom") + + pm = make_pm(FailingAsyncPlugin()) + + async def run_test() -> None: + await pm.run_async(lambda: pm.hook.test_hook()) + + with pytest.raises(ValueError, match="async boom"): + asyncio.run(run_test()) + + +def test_run_async_without_greenlet() -> None: + try: + import greenlet # noqa: F401 + + pytest.skip("greenlet is available, cannot test the error case") + except ImportError: # pragma: no cover + pm = make_pm() + + async def run_test() -> None: + with pytest.raises(RuntimeError, match="greenlet is required"): + await pm.run_async(lambda: "test") + + asyncio.run(run_test()) + + +def test_require_await_outside_context() -> None: + submitter = Submitter() + + async def coro() -> None: + pass # pragma: no cover + + awaitable = coro() + with pytest.raises(RuntimeError, match="outside of async context"): + submitter.require_await(awaitable) + awaitable.close() + + +def test_maybe_submit_outside_context_returns_awaitable() -> None: + submitter = Submitter() + + async def coro() -> str: + return "value" + + awaitable = coro() + assert submitter.maybe_submit(awaitable) is awaitable + assert asyncio.run(awaitable) == "value" + + +def test_async_generator_to_sync_basic() -> None: + pytest.importorskip("greenlet") + + async def simple_async_gen() -> AsyncGenerator[str]: + yield "first" + yield "second" + + submitter = Submitter() + + def test_func() -> tuple[list[str], Any]: + sync_gen = async_generator_to_sync(simple_async_gen(), submitter) + values = [] + try: + while True: + values.append(next(sync_gen)) + except StopIteration as e: + return values, e.value + + values, final_value = asyncio.run(submitter.run(test_func)) + assert values == ["first", "second"] + assert final_value is None + + +def test_async_generator_to_sync_with_send() -> None: + pytest.importorskip("greenlet") + + async def async_gen_with_send() -> AsyncGenerator[str, str]: + sent1 = yield "initial" + sent2 = yield f"got_{sent1}" + yield f"final_{sent2}" + + submitter = Submitter() + + def test_func() -> tuple[str, str, str, Any]: + sync_gen = async_generator_to_sync(async_gen_with_send(), submitter) + value1 = next(sync_gen) + value2 = sync_gen.send("hello") + value3 = sync_gen.send("world") + try: + next(sync_gen) + raise AssertionError("should have raised StopIteration") + except StopIteration as e: + return value1, value2, value3, e.value + + value1, value2, value3, final = asyncio.run(submitter.run(test_func)) + assert value1 == "initial" + assert value2 == "got_hello" + assert value3 == "final_world" + assert final is None + + +def test_async_generator_to_sync_with_exception() -> None: + pytest.importorskip("greenlet") + + async def async_gen_with_exception() -> AsyncGenerator[str]: + try: + yield "before_exception" + yield "should_not_reach" + except ValueError as e: + yield f"caught_{e}" + + submitter = Submitter() + + def test_func() -> tuple[str, str, Any]: + sync_gen = async_generator_to_sync(async_gen_with_exception(), submitter) + value1 = next(sync_gen) + value2 = sync_gen.throw(ValueError("test_error")) + try: + next(sync_gen) + raise AssertionError("should have raised StopIteration") + except StopIteration as e: + return value1, value2, e.value + + value1, value2, final = asyncio.run(submitter.run(test_func)) + assert value1 == "before_exception" + assert value2 == "caught_test_error" + assert final is None + + +def test_async_generator_to_sync_inactive_submitter() -> None: + async def simple_async_gen() -> AsyncGenerator[str]: + yield "test" # pragma: no cover + + submitter = Submitter() + + with pytest.raises(RuntimeError, match="outside of async context"): + sync_gen = async_generator_to_sync(simple_async_gen(), submitter) + next(sync_gen) + + +def test_submitter_repr() -> None: + submitter = Submitter() + assert repr(submitter) == "" diff --git a/testing/test_multicall.py b/testing/test_multicall.py index 5e110d3f..698089b8 100644 --- a/testing/test_multicall.py +++ b/testing/test_multicall.py @@ -9,6 +9,7 @@ from pluggy import HookspecMarker from pluggy import NormalImpl from pluggy import WrapperImpl +from pluggy._async import Submitter from pluggy._callers import _multicall @@ -30,7 +31,9 @@ def MC( wrapper_impls.append(f) else: normal_impls.append(f) - return _multicall("foo", normal_impls, wrapper_impls, kwargs, firstresult) + return _multicall( + "foo", normal_impls, wrapper_impls, kwargs, firstresult, Submitter() + ) def test_keyword_args() -> None: diff --git a/uv.lock b/uv.lock index 89422ba1..237a937e 100644 --- a/uv.lock +++ b/uv.lock @@ -167,6 +167,92 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/81/47/dd9a212ef6e343a6857485ffe25bba537304f1913bdbed446a23f7f592e1/filelock-3.29.0-py3-none-any.whl", hash = "sha256:96f5f6344709aa1572bbf631c640e4ebeeb519e08da902c39a001882f30ac258", size = 39812, upload-time = "2026-04-19T15:39:08.752Z" }, ] +[[package]] +name = "greenlet" +version = "3.5.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/3e/6e/0091f175ccd02b02bc8811bbcbcc6ac2e980be116e3b2f7a736ca322bf84/greenlet-3.5.6.tar.gz", hash = "sha256:8e67c43bdfc88d5fee6db0d3e40175b362fc95fb85f0412d233b9b203c53a575", size = 207653, upload-time = "2026-09-14T15:42:51.806Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/a7/4f/4af258e1ce388eb64be82d6466b7d0b3bc3eede154e14c3360a76a759b71/greenlet-3.5.6-cp310-cp310-macosx_11_0_universal2.whl", hash = "sha256:95e7c44d072db623a1aab04ce488cf9533294a77ed9d072cd503a3596f4106ac", size = 292967, upload-time = "2026-09-14T14:26:34.475Z" }, + { url = "https://files.pythonhosted.org/packages/1d/05/dc0d54e90af2f192724936799cee990687792ff54a4638d3ae0e0a0145c0/greenlet-3.5.6-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:b7d501d5eb5d4f67207df364752ad697465b834268744be7581c18d81d35d41d", size = 609284, upload-time = "2026-09-14T15:11:58.49Z" }, + { url = "https://files.pythonhosted.org/packages/c1/51/826b4fe7bc39c3f910c95e889a981c827f72a611fc035f31816a256cf043/greenlet-3.5.6-cp310-cp310-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:a364c1ea75dc51b83a17f52fe0c79cf8bc4ddf740403bebd4581c7666eea017d", size = 622648, upload-time = "2026-09-14T15:20:39.198Z" }, + { url = "https://files.pythonhosted.org/packages/55/3b/f2d36fc38934588dff6d6ec9288934c9611abac7a79c866c2002fc707698/greenlet-3.5.6-cp310-cp310-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:5599b380c1f28efeb724e81569eac80cd92f99a85bd9775456caaf3225d40b11", size = 629557, upload-time = "2026-09-14T15:25:03.036Z" }, + { url = "https://files.pythonhosted.org/packages/94/5c/092682ae7ca1aadd44aa36b0bc35c48c9ed55f9ffebb5794980b1e2ae74b/greenlet-3.5.6-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:eed88b64a5e5da72d6a71cdc5aaeefaa5ced9b748f8d19f89800b339961dad39", size = 622813, upload-time = "2026-09-14T14:35:54.845Z" }, + { url = "https://files.pythonhosted.org/packages/d6/f6/4167f0e44e795840cbc219b2e5d8f576492192cd9e3e17e827db20287daa/greenlet-3.5.6-cp310-cp310-manylinux_2_39_riscv64.whl", hash = "sha256:5bbda3c70dd35d60671bc33b01916802707a052130d9e50cdb871d34594d35cb", size = 425482, upload-time = "2026-09-14T15:28:34.144Z" }, + { url = "https://files.pythonhosted.org/packages/07/59/9c6b723a559b53bee0980a5dc57577d434891dd454f0fe54ed6e22d4e4ce/greenlet-3.5.6-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:874cea8bb1ec1ddccbacbd027856f6bf496f6bc18aba97a918c20e067edab236", size = 1585817, upload-time = "2026-09-14T15:10:03.922Z" }, + { url = "https://files.pythonhosted.org/packages/de/d4/2bceaa305ff0ecf99511480da8f0a866eb2469a68da8715008a0d3e8f65f/greenlet-3.5.6-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:128813fc29f2336a21b4d06eedd5e16bcc7ea46f59e9ff1cb30ea70e48195d88", size = 1650177, upload-time = "2026-09-14T14:35:46.736Z" }, + { url = "https://files.pythonhosted.org/packages/7a/33/c57855a6abada0c7bfb9c6c0f33df1fbed8e5224708478dbcdff6c5db490/greenlet-3.5.6-cp310-cp310-win_amd64.whl", hash = "sha256:dad3d233d441a022c1f7155f0fb9d5aff7b97c1ea8c7dfa02cce586b16ab2d0b", size = 322896, upload-time = "2026-09-14T14:24:52.308Z" }, + { url = "https://files.pythonhosted.org/packages/f1/d7/41511ee2696f14be4200b524d9553dc4295e2bdeb20aa8962c3cb25e71c6/greenlet-3.5.6-cp311-cp311-macosx_11_0_universal2.whl", hash = "sha256:a6a4b98a9132e0f45c9fc245a63894cfd8c45fb7a0d6bffc5eab3ec327cf7324", size = 294075, upload-time = "2026-09-14T14:25:16.922Z" }, + { url = "https://files.pythonhosted.org/packages/f8/7b/b509624970909294cd064ff7346148ca9941c21bec9026d7873dd254e9fa/greenlet-3.5.6-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:45bfd2b51e38aaa5f9849f114d9c7c1d75f69187c849b3549cd64c465283abfa", size = 613429, upload-time = "2026-09-14T15:12:00.454Z" }, + { url = "https://files.pythonhosted.org/packages/2b/5c/d2eb503067f9ba20875ef8c87681f29a64f53bbbbe4059a5d7c53179d442/greenlet-3.5.6-cp311-cp311-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:3c6dede9133e1da41d561bc3fb14e92b47e2ce39ae60edefaad145658ea7c5e2", size = 625405, upload-time = "2026-09-14T15:20:41.053Z" }, + { url = "https://files.pythonhosted.org/packages/1b/24/9b071d11c8bb9f5f38cccacc38fcc234d91997a4c395cc2bf43ecae89642/greenlet-3.5.6-cp311-cp311-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:4fb8e59f68845d56c23c031dcd79c329f345e4a9d2ffac91c3d1ab366bdc457b", size = 633270, upload-time = "2026-09-14T15:25:04.864Z" }, + { url = "https://files.pythonhosted.org/packages/ec/d3/63d4477ce31dff2fd802a9a20240f6606aac85977e0fb18443aae33de3f6/greenlet-3.5.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1c20ea32a73d17b9b60e3371240e17b0068120c98a5ec01a224a7dd8c89733ba", size = 624428, upload-time = "2026-09-14T14:35:56.895Z" }, + { url = "https://files.pythonhosted.org/packages/88/17/ac11883ecc9da19c681c8b763ee39e6f7dca2aa81874eb11a075d3cbeb00/greenlet-3.5.6-cp311-cp311-manylinux_2_39_riscv64.whl", hash = "sha256:d701eab36200c36224833d07dbdb709adb7fd4253429548ddb5e547b8ed40586", size = 428068, upload-time = "2026-09-14T15:28:35.872Z" }, + { url = "https://files.pythonhosted.org/packages/ad/aa/9cde4e00688eaa2a03b91d12e4681439a87e6aad860399e0847af6a014ca/greenlet-3.5.6-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:5a0b2791239c99992a86c1b635b787fe2a877d9eaaa26f8891ce943832b585ae", size = 1588385, upload-time = "2026-09-14T15:10:05.386Z" }, + { url = "https://files.pythonhosted.org/packages/5c/01/24632b5ec186b64e21e07a8f53ce5e15a7e9cb33eddee99a5fe16379afa5/greenlet-3.5.6-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:188bf333769b7145e2b0b4a7f09615ec550ed44d3a2a8395fb7b36f0e9901e13", size = 1653119, upload-time = "2026-09-14T14:35:48.275Z" }, + { url = "https://files.pythonhosted.org/packages/ce/6c/019d2ef898f4b9ac845167f1c6f73229e9a4e2439362a5e2ce50205a19b0/greenlet-3.5.6-cp311-cp311-win_amd64.whl", hash = "sha256:a6b4ff33f7e011bbaa148238d131c4fd4f8afbab3c104ddfbdb2b12b74ff7016", size = 323317, upload-time = "2026-09-14T14:22:38.836Z" }, + { url = "https://files.pythonhosted.org/packages/5a/7a/439df999455e3bdf02b1c68f3848d4020385ef0a01f89f706b07bf148a65/greenlet-3.5.6-cp311-cp311-win_arm64.whl", hash = "sha256:59deccd347735a7774223b05a93773fddbb298aba3cea21be4337fb4752dbe32", size = 307739, upload-time = "2026-09-14T14:23:40.469Z" }, + { url = "https://files.pythonhosted.org/packages/72/18/3fc6d951466ae9a2a688edcddde3b2e388da0a8244e0caf7117bbeb0eb95/greenlet-3.5.6-cp312-cp312-macosx_11_0_universal2.whl", hash = "sha256:a5876d0a60355af98d535c47f6cd6eb0f8a432396dab26845d380b92f8412422", size = 295668, upload-time = "2026-09-14T14:22:33.241Z" }, + { url = "https://files.pythonhosted.org/packages/27/89/366d2af5061eeefa5012f510d95a99c8620dcc457609838db4d538820318/greenlet-3.5.6-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e85880b538e59a59f55117b81f208a6660ad5ac328aad9305f812d9b8bc67a0f", size = 611700, upload-time = "2026-09-14T15:12:01.962Z" }, + { url = "https://files.pythonhosted.org/packages/54/1c/07f133f865fd58ae593dd2bbec3144acaee9b04ffe2eb48c6e121747ceef/greenlet-3.5.6-cp312-cp312-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:f0ba7c2a329d650628f4c8572fd1db29f0a59dd70a3e3e0710dcf18a35cce9d8", size = 624223, upload-time = "2026-09-14T15:20:42.459Z" }, + { url = "https://files.pythonhosted.org/packages/a7/f2/844dc823ff2752ad049caa6b59d57e4572f9c445934b02d3518f4c67197c/greenlet-3.5.6-cp312-cp312-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:ee7d9da3bf493909cf811a3f038840cb34fab5ae2956b8a263919f6e289ab188", size = 629529, upload-time = "2026-09-14T15:25:06.354Z" }, + { url = "https://files.pythonhosted.org/packages/66/6a/1594f3869c57c149abdb380492529e04d4c0229b5e4d79572c5bd0aaa673/greenlet-3.5.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:975736b002ed080d124cf81a79cb7e05cb26d6b3f5c7a7b651c0fcce70353aa1", size = 621404, upload-time = "2026-09-14T14:35:59.027Z" }, + { url = "https://files.pythonhosted.org/packages/c0/42/b1f8dbc89a53b9e77859fc1ad1627d106fc361daa3ea4bdf43a91ebb4338/greenlet-3.5.6-cp312-cp312-manylinux_2_39_riscv64.whl", hash = "sha256:71890d5247020c25c21a6b65202782bfc281d4e6e244842419d30e3492bb6dcc", size = 432385, upload-time = "2026-09-14T15:28:37.369Z" }, + { url = "https://files.pythonhosted.org/packages/a2/f5/33e5c9e48178b9259fd000f8f45caa4a65036f65d3d0c06a602f570f025d/greenlet-3.5.6-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:0616b8f878098c5681fd8f0dc92d887551717402342a70f0abcbfea5f5ad8a44", size = 1584998, upload-time = "2026-09-14T15:10:06.653Z" }, + { url = "https://files.pythonhosted.org/packages/ef/31/9b4e140bc24d0ad7927ebd651f5608b0acc2334d061748c3b6ad19085cfa/greenlet-3.5.6-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:3dbb4596a6a4e5d47121a33ff20533a81e60f302d9e67b69909a8bc21a43f0a7", size = 1647568, upload-time = "2026-09-14T14:35:49.787Z" }, + { url = "https://files.pythonhosted.org/packages/c3/71/d79f1791f824f8ff15c2978746640467ae932a2365e0201069f7f272395f/greenlet-3.5.6-cp312-cp312-win_amd64.whl", hash = "sha256:7ac4abb3877c43af320392c664774eef6fa2cc063c79a55fc02d844a3cbe7395", size = 324203, upload-time = "2026-09-14T14:22:54.504Z" }, + { url = "https://files.pythonhosted.org/packages/63/af/42aca4d56e8cb321912203069d8d34734cb288222f10ad2ae102718cc577/greenlet-3.5.6-cp312-cp312-win_arm64.whl", hash = "sha256:301102a49120b095e72a7838792b41233975fc1c155daec6d98f81c00c9280e0", size = 308310, upload-time = "2026-09-14T14:24:03.008Z" }, + { url = "https://files.pythonhosted.org/packages/f1/a1/e720a38852366c589e1a46cf570b886507ad2cf591050c203365638baab0/greenlet-3.5.6-cp313-cp313-macosx_11_0_universal2.whl", hash = "sha256:f96f0e30b5a95c7631b12bfe214cbc90ec8fe8cfa36920596c10514a65743519", size = 294627, upload-time = "2026-09-14T14:24:40.102Z" }, + { url = "https://files.pythonhosted.org/packages/eb/c3/58187858df41354a11e6a55b421e7af9059798abdab3a384cc51b8567c38/greenlet-3.5.6-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c75116c9de79949de23006e2d9b35ee82874c594fcf5c0311b439acaa14b8441", size = 614356, upload-time = "2026-09-14T15:12:03.399Z" }, + { url = "https://files.pythonhosted.org/packages/ce/b9/3a7e67d5f05c9760b1ad411fa52264bd69cc08e22a2ebfb4018b90628ced/greenlet-3.5.6-cp313-cp313-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:cad5782f93f7f738b62c6527b6f32a60694d924029f299a8b524758cfa53d815", size = 626756, upload-time = "2026-09-14T15:20:44.269Z" }, + { url = "https://files.pythonhosted.org/packages/c6/7c/40400455f5b5a65bb83e94fde66d1be9e5ec518638113f8083ace746c309/greenlet-3.5.6-cp313-cp313-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a93ee7c6e8fd0f8a83525a51bd777be57ee17787e91d805bd8d6faf9dcada18e", size = 632632, upload-time = "2026-09-14T15:25:07.813Z" }, + { url = "https://files.pythonhosted.org/packages/85/cb/ab0c123c514ed4e94c0dc9ee2e86362633e6b998cfc05de7fc9ac2eb9690/greenlet-3.5.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f98e8215e172f567ce80eeaed9107fb4d32b6c44f26983d9b8334658136a205a", size = 623779, upload-time = "2026-09-14T14:36:01.104Z" }, + { url = "https://files.pythonhosted.org/packages/f9/67/1f35cff30a6c51c3f23b63d4afcc7313ab4f97490ba3676fa78178984b27/greenlet-3.5.6-cp313-cp313-manylinux_2_39_riscv64.whl", hash = "sha256:7f731ebac68ea06d628658295cb2d217b10186329fcf9a3b6a149045059bf92e", size = 434933, upload-time = "2026-09-14T15:28:38.858Z" }, + { url = "https://files.pythonhosted.org/packages/a5/26/fda8a5a06e7073333ccb038133c5893b9e0c4fe29d5992a17e83c241bc6e/greenlet-3.5.6-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:df19e2d0b1620039af5102563fbd96e8938c7f5c3f5828528d641d9fc585525e", size = 1584930, upload-time = "2026-09-14T15:10:08.234Z" }, + { url = "https://files.pythonhosted.org/packages/2f/37/50f8813163148d6234e08b23dcad6a9e37f01d148c8ec976e4c44ea2d918/greenlet-3.5.6-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:06c0e933290fba8ffe53ead4ae1b8044b0e9754b75cebf381aa2bc3e50d82fac", size = 1647590, upload-time = "2026-09-14T14:35:51.173Z" }, + { url = "https://files.pythonhosted.org/packages/86/da/b7669b09586365654083a62bd0724cf06cb74bd5085a15cdd161271f992f/greenlet-3.5.6-cp313-cp313-win_amd64.whl", hash = "sha256:5b602b4201b965a8354d74e232364a66ff243dd142e350d035f46169bb36e13d", size = 324086, upload-time = "2026-09-14T14:23:48.428Z" }, + { url = "https://files.pythonhosted.org/packages/e5/5d/c9663cfe84a2a9e0aa96f066f5b0594c227ea4c647511e087e2e11d4ac0a/greenlet-3.5.6-cp313-cp313-win_arm64.whl", hash = "sha256:876077e7ebb8c84ed068e2b23d4c62ebb010d60df84b9591af1be2f39010ffb2", size = 308211, upload-time = "2026-09-14T14:28:01.634Z" }, + { url = "https://files.pythonhosted.org/packages/66/c0/d254544ae2b8bdd311aef000fafc02828c2771b17d994b3075620ea7cc6e/greenlet-3.5.6-cp314-cp314-macosx_11_0_universal2.whl", hash = "sha256:8cddea1b8339451c2fb3388e138347b6126744f33b611bdb55b7357361cfef46", size = 295221, upload-time = "2026-09-14T14:25:11.583Z" }, + { url = "https://files.pythonhosted.org/packages/18/18/eb54be16b9cc3971e09ca5b73334e1b8c804a4630d9addaaf218a4fe300f/greenlet-3.5.6-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c59acfa8eb73a1e0d484392dc002bdf001fd4ce73394e0132df3d1ab6093d7cb", size = 660992, upload-time = "2026-09-14T15:12:04.876Z" }, + { url = "https://files.pythonhosted.org/packages/8f/b4/e193efe65671dcf294bc51fcc59efb52d154adf8612c4ea016da0d2c486c/greenlet-3.5.6-cp314-cp314-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:a3b4a01c6da07ef9f80d4fe8933b994bc99747bcea3eab0330a9c34d3c12655b", size = 673428, upload-time = "2026-09-14T15:20:45.756Z" }, + { url = "https://files.pythonhosted.org/packages/fd/21/631bb45fafde1dca782152377c0676d182ec924820064047f533a3627b28/greenlet-3.5.6-cp314-cp314-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:dd0b83bed3405b586a3133629f1d1a5bc7bfd64822a3b7ab342bdc68e6dbc61b", size = 677688, upload-time = "2026-09-14T15:25:09.279Z" }, + { url = "https://files.pythonhosted.org/packages/45/ac/28fa7a9e50f2859466214c4ac584d776db52c1604ad4dd158960a5af2a1f/greenlet-3.5.6-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9a09d59bef1db94f384b5bcc2d523694d338f3df6b757aeeaf7baca5d0c0be88", size = 670773, upload-time = "2026-09-14T14:36:02.577Z" }, + { url = "https://files.pythonhosted.org/packages/40/30/2b0a73e68e1e18e30b601d0d183cfdfc2beca4de5a6843c630f0fc9fb90c/greenlet-3.5.6-cp314-cp314-manylinux_2_39_riscv64.whl", hash = "sha256:fdacf26402389bdd89857ad3c045a26fe8f3314f9a8b28226f82f88463a65b77", size = 480475, upload-time = "2026-09-14T15:28:40.741Z" }, + { url = "https://files.pythonhosted.org/packages/c3/cd/fb7d6cdd86ff3427c1494854f0e35437eba05142be91f530f6da75e09e19/greenlet-3.5.6-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:8b7c73d1cef3d9ae963e9ff03f6222df43efbb9054ffd2f1969c935b7fc84c02", size = 1631900, upload-time = "2026-09-14T15:10:09.745Z" }, + { url = "https://files.pythonhosted.org/packages/f6/40/143bdbb20a516628cb15074ae52ed17d850b450292609c7a6fccac6dbece/greenlet-3.5.6-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:8b27df301f56e3b3d2298095c8f7d6b68f2521f6b1693e901fa039bdbae34424", size = 1693740, upload-time = "2026-09-14T14:35:52.959Z" }, + { url = "https://files.pythonhosted.org/packages/c9/9e/019642432e6ae283301df1361227d47610709d2dc69a38f95edef266d713/greenlet-3.5.6-cp314-cp314-win_amd64.whl", hash = "sha256:f8f0bd690e1a41294ac87905e8121c81a3761ec2583c768f13467428606c8c7a", size = 327473, upload-time = "2026-09-14T14:28:12.948Z" }, + { url = "https://files.pythonhosted.org/packages/e9/7f/8aafc7bf70c948786dba7221d0dc0838e5329bebc6d434ef2208b4f0e760/greenlet-3.5.6-cp314-cp314-win_arm64.whl", hash = "sha256:8cda13494d86a4f12429641117cb6ac4bbbc9c30a33f711f7d3a2e5fbe4b0b7e", size = 311095, upload-time = "2026-09-14T14:28:00.7Z" }, + { url = "https://files.pythonhosted.org/packages/14/7e/7a205688a5b3074933b18a906608d46d106e9a79d776bdab5a4abf4b4feb/greenlet-3.5.6-cp314-cp314t-macosx_11_0_universal2.whl", hash = "sha256:97c5a53e8c1754df58e73f047a99e287d4da1bdfe64b0072fb25c87000897951", size = 305352, upload-time = "2026-09-14T14:21:31.962Z" }, + { url = "https://files.pythonhosted.org/packages/78/cb/9c4a57a9d9dd0256e20b8f7f4f06554c2c92badebf0ab73ce344321b78b9/greenlet-3.5.6-cp314-cp314t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:fea4427d1ffdb3b523d7daa6712038428a4c16c450b9777bdd1221cfee0eab49", size = 672671, upload-time = "2026-09-14T15:12:06.347Z" }, + { url = "https://files.pythonhosted.org/packages/97/52/c6729681ebbd298f4decd28746815acc8a0b0a0fde21d2df33776fd4d042/greenlet-3.5.6-cp314-cp314t-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:73a29b5ba642e35433166a03a3e02935e7238c4b3467fbd77523b99edea23e5b", size = 679489, upload-time = "2026-09-14T15:20:47.291Z" }, + { url = "https://files.pythonhosted.org/packages/71/76/3c11c21e0716b1f1dc7c1a4b3d690abb1d3b448c69a9d32049fecb64010a/greenlet-3.5.6-cp314-cp314t-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:61a61b4a95a4f97922c3a6f5606d3e360851584bd47e500a5161373c53810e3d", size = 681303, upload-time = "2026-09-14T15:25:11.088Z" }, + { url = "https://files.pythonhosted.org/packages/58/c5/2b6c721ba8b8963da42d5a0f57f25b8aaeb1fe9bdd156875e57f3be648a2/greenlet-3.5.6-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:460e70b033aba8ed47e2ac9b5d0d2157b05a34fbfa30a241400aef4118902cdc", size = 676608, upload-time = "2026-09-14T14:36:03.959Z" }, + { url = "https://files.pythonhosted.org/packages/3f/26/3ae402202452cd5941bbbd483e5a74297e2397e7aa3182c2a5e3ab7d5666/greenlet-3.5.6-cp314-cp314t-manylinux_2_39_riscv64.whl", hash = "sha256:fe3170a69fe039b18ad18171e66faa9a75f6fe9d78f968fd9b54e09fbd714d81", size = 510112, upload-time = "2026-09-14T15:28:42.112Z" }, + { url = "https://files.pythonhosted.org/packages/b2/04/0d018e0d05bcdde19a0fcb907834155f1fc853a9bedd3f3f5e6acadcae19/greenlet-3.5.6-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:ca80a49b53ed1d22f7282da7255f7bb2fd1935fd0f623d8613fda38745f18961", size = 1641479, upload-time = "2026-09-14T15:10:11.216Z" }, + { url = "https://files.pythonhosted.org/packages/59/bb/f02ef9073919158f6403fe3701d4ed4403d646720e7201dfc6e9d264bac3/greenlet-3.5.6-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:916f92f2a8db10508f739d0b5e00b83defe5d1115a997c54532a6d7cf8c95404", size = 1698758, upload-time = "2026-09-14T14:35:54.336Z" }, + { url = "https://files.pythonhosted.org/packages/08/a5/1f48fe647473a2dcccfd1839b2ff2c78eb57009be776b4da071e901c9bff/greenlet-3.5.6-cp314-cp314t-win_amd64.whl", hash = "sha256:886bcf1870af74c32bc310fd00a6b803445e17e51b7d5a107c7b35c0f362cc16", size = 331574, upload-time = "2026-09-14T14:27:18.451Z" }, + { url = "https://files.pythonhosted.org/packages/cd/72/3882855a75838faeb54a58aeef4fd77d20b2a86d4bad570c70d41b565dcf/greenlet-3.5.6-cp315-cp315-macosx_11_0_universal2.whl", hash = "sha256:3ac3494c381dab876cad7d0b22f3a722f3e0c8deb3a65b9e7f35ad7f58b8fcb3", size = 295926, upload-time = "2026-09-14T14:27:21.16Z" }, + { url = "https://files.pythonhosted.org/packages/10/1f/be4d957d8a9b90bcbe8db206548a42134d96222d43e5ed3fc4708fb6e24b/greenlet-3.5.6-cp315-cp315-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:602024dae6d77e161f4b89491b62ca1d4f19949d79d47b2db057e476d21179d6", size = 666910, upload-time = "2026-09-14T15:12:07.901Z" }, + { url = "https://files.pythonhosted.org/packages/a1/af/60d62571a7d6de961e4ce7625d6c2faf359345659fc782d2cdf517c34577/greenlet-3.5.6-cp315-cp315-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:f8e63209c3e1e828ee6a457529b4a6d8b05d050fe0ae03a7ae49e967c5d312e0", size = 677415, upload-time = "2026-09-14T15:20:48.817Z" }, + { url = "https://files.pythonhosted.org/packages/f5/41/b3114c97c10e796010f00a30f51c81470072bca4b53e396ccca87484fcf7/greenlet-3.5.6-cp315-cp315-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:9133d68624b1f2e89ec2f554d56aea8a5b0d7168cd9320200ba58d4d794845a4", size = 681337, upload-time = "2026-09-14T15:25:12.812Z" }, + { url = "https://files.pythonhosted.org/packages/fb/16/ac9e547b611539aaed1870eb1d6ddc57abdd5924b3a99bb9b5f0b44176b8/greenlet-3.5.6-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ccadce0130fd813ec86ebfe969a6c58b42acc1d0fe55a47525375b740e07b605", size = 675927, upload-time = "2026-09-14T14:36:05.34Z" }, + { url = "https://files.pythonhosted.org/packages/48/1b/d41861c2fa00968e39e467a495ca8db9ce9b6310a5d9b57561b3d0dc48fa/greenlet-3.5.6-cp315-cp315-manylinux_2_39_riscv64.whl", hash = "sha256:5adcbbfe78bdc242c71740a02e0991cc1b2f34d33c8bb15ca45eee8fd1140942", size = 487339, upload-time = "2026-09-14T15:28:43.497Z" }, + { url = "https://files.pythonhosted.org/packages/c4/b1/b7ba08d6431121741f1d30be0d5d292e76873325179a63586cd9217b62f6/greenlet-3.5.6-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:9297fb9c39b9a2c039dbcd306c410bd6906b95244dec3bba4318d36c718c164c", size = 1637236, upload-time = "2026-09-14T15:10:12.442Z" }, + { url = "https://files.pythonhosted.org/packages/af/c5/3b1cbc68f0c082022fc8717f7fe4b8b13b8d583c52352be37f4e9f55bcd2/greenlet-3.5.6-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:b374e79ffa7511afc11773aef40a4ccea6191fba1c856ea2f9c56738dca69d7a", size = 1698526, upload-time = "2026-09-14T14:35:56.039Z" }, + { url = "https://files.pythonhosted.org/packages/de/56/12941ed2711400451c89d544e10f831800a2770f19dd55eac8f0f7f2003b/greenlet-3.5.6-cp315-cp315-win_amd64.whl", hash = "sha256:7969bffa322c097bd46ae595ada6a931cefda613f18ba64587e9cff4cb320756", size = 327739, upload-time = "2026-09-14T14:23:55.768Z" }, + { url = "https://files.pythonhosted.org/packages/c5/3b/576b9ed5ac929252e340cf60b4bcb6a8515350dc20797064b1922dc4ea75/greenlet-3.5.6-cp315-cp315-win_arm64.whl", hash = "sha256:8dba0129b93e7091dfefaf4cf7000172741bff7f47bf6326fcf17f32fbb54d6b", size = 311715, upload-time = "2026-09-14T14:28:25.154Z" }, + { url = "https://files.pythonhosted.org/packages/16/c2/86cfc5555a98e12b86966ddbd24fd39af32f71f2f785c6595b7feb2db156/greenlet-3.5.6-cp315-cp315t-macosx_11_0_universal2.whl", hash = "sha256:de3de000d459402cda015068fd135aa50c0bf6f2477a80d4da1e646f123b4e78", size = 306244, upload-time = "2026-09-14T14:27:57.565Z" }, + { url = "https://files.pythonhosted.org/packages/14/6d/83ffc9d05a75a80ab3a7595dbb1d9604e5d4fc2996d73a8ae2dbd1284900/greenlet-3.5.6-cp315-cp315t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:45663c01a4de48b9a64a2ee1509d92d1dfd3afb02b2ccfc9333029d11aef996a", size = 676578, upload-time = "2026-09-14T15:12:09.468Z" }, + { url = "https://files.pythonhosted.org/packages/5d/d6/c2cf684810e5caded075970aaadea654ecb58b8382b9aecf1d231b936894/greenlet-3.5.6-cp315-cp315t-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:3deccbb57a481e3a408fe61cdfd5c13e0678fc0a30fdd09597917ca87b4be877", size = 684203, upload-time = "2026-09-14T15:20:50.261Z" }, + { url = "https://files.pythonhosted.org/packages/f2/d1/039c353d5593a97a89699e989324c9bc86af499e6c6152fe0180f5742204/greenlet-3.5.6-cp315-cp315t-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:63aff70fe5aac59c72215f42ec39fcb59ff46774fa966e717f8ecb6ee2273577", size = 686023, upload-time = "2026-09-14T15:25:14.528Z" }, + { url = "https://files.pythonhosted.org/packages/62/19/00e1bee5d2af890dc8f400b54d0b0f9b489965f92bc12b407ff72cc6f469/greenlet-3.5.6-cp315-cp315t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:311018b46472fb26ee85870847fb89eb64cc8aaddb617400789d87076f7cfeec", size = 681253, upload-time = "2026-09-14T14:36:06.742Z" }, + { url = "https://files.pythonhosted.org/packages/8a/62/97ceb8e0b2ea96046cdf8e95b042715020ebb12d83ea0690db80a8f03d23/greenlet-3.5.6-cp315-cp315t-manylinux_2_39_riscv64.whl", hash = "sha256:520648db8fb92eef7b3e6013f5a6f901cdf0d6685f639c2f7a245879f865bef7", size = 517058, upload-time = "2026-09-14T15:28:44.924Z" }, + { url = "https://files.pythonhosted.org/packages/89/58/c9275fd0ca195d1d3402931bcce8cfcc74726ff76efb1883d229e6e1a3d7/greenlet-3.5.6-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:7f924a5a9d5890649566f2f6682e0d8ad8ca23028bacffbbac36dbd7fd680176", size = 1645988, upload-time = "2026-09-14T15:10:13.758Z" }, + { url = "https://files.pythonhosted.org/packages/e0/36/b35747582fa4f1a5453f8f3002405dbac788e450cec7674dc2d204b6ccb5/greenlet-3.5.6-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:de9923832f2d8c1a5ecd8d7260465a6ca5a86888a0d129e3bd5cf0406d2fc5bf", size = 1702383, upload-time = "2026-09-14T14:35:58.143Z" }, + { url = "https://files.pythonhosted.org/packages/ed/69/6ec22ac9351e474d2a134d0ff9400dc80362d1c20f0721088ffffdfc205b/greenlet-3.5.6-cp315-cp315t-win_amd64.whl", hash = "sha256:2ab5f42ac6c238eb71770715e6e909ad9a1a92b6c681ccb64cd5a0f07edb953f", size = 331845, upload-time = "2026-09-14T14:27:41.723Z" }, + { url = "https://files.pythonhosted.org/packages/30/cf/697c051fd534e223461fb8b523890e21a24eeca229cd50624cff6f02fabd/greenlet-3.5.6-cp315-cp315t-win_arm64.whl", hash = "sha256:f9fe868463ec7e1363733af77e38a5fda3e9b63940337048c945d69e0c80ff24", size = 315070, upload-time = "2026-09-14T14:22:21.476Z" }, +] + [[package]] name = "identify" version = "2.6.19" @@ -216,22 +302,31 @@ wheels = [ name = "pluggy" source = { editable = "." } +[package.optional-dependencies] +async = [ + { name = "greenlet" }, +] + [package.dev-dependencies] dev = [ { name = "pre-commit" }, ] testing = [ { name = "coverage" }, + { name = "greenlet" }, { name = "pytest" }, { name = "pytest-benchmark" }, ] [package.metadata] +requires-dist = [{ name = "greenlet", marker = "extra == 'async'" }] +provides-extras = ["async"] [package.metadata.requires-dev] dev = [{ name = "pre-commit" }] testing = [ { name = "coverage" }, + { name = "greenlet" }, { name = "pytest", specifier = ">=8" }, { name = "pytest-benchmark" }, ] From d1b2fcbc2541147f75ea0b27076880c844c324c1 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 9 Sep 2026 22:07:12 +0200 Subject: [PATCH 2/2] fix(async): close awaitables require_await declines to await require_await raised before awaiting, so the awaitable handed to it was never awaited and never closed. async_generator_to_sync calls it as require_await(async_gen.__anext__()), which meant the orphaned coroutine was created inside the library and no caller could reach it to clean up. CPython emitted "RuntimeWarning: coroutine method 'asend' ... was never awaited" while finalizing the async generator; the pytest config's filterwarnings = error turned that into an exception inside __del__, surfacing as an unraisable that failed the 3.13 and 3.14 jobs. require_await now takes ownership of the awaitable either way, closing it before raising when inactive. This matches maybe_submit, which hands the awaitable back to the caller when inactive rather than dropping it. Use getattr for close since Awaitable does not guarantee it. test_require_await_outside_context papered over this with a manual close(); it now asserts the coroutine was closed, which fails without the fix on every supported version rather than only on 3.13+. Co-Authored-By: Claude Opus 5 (1M context) Co-Authored-By: Claude Code --- src/pluggy/_async.py | 12 +++++++++++- testing/test_async.py | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/pluggy/_async.py b/src/pluggy/_async.py index e5a00970..83190cb9 100644 --- a/src/pluggy/_async.py +++ b/src/pluggy/_async.py @@ -67,12 +67,22 @@ def maybe_submit(self, coro: Awaitable[_T]) -> _T | Awaitable[_T]: return coro def require_await(self, coro: Awaitable[_T]) -> _T: - """Await an awaitable, raising an error if not in async context.""" + """Await an awaitable, raising an error if not in async context. + + Ownership of ``coro`` is taken either way: when inactive it is closed + before raising, so callers never leak a coroutine that would later + warn about never having been awaited. + """ active = self._active_submitter if active is not None: res: _T = active.switch(coro) return res else: + # Not going to await it, so dispose of it rather than let it be + # collected unawaited (a RuntimeWarning raised inside __del__). + close = getattr(coro, "close", None) + if close is not None: + close() raise RuntimeError("require_await called outside of async context") async def run(self, sync_func: Callable[[], _T]) -> _T: diff --git a/testing/test_async.py b/testing/test_async.py index 2b8c790e..b3510831 100644 --- a/testing/test_async.py +++ b/testing/test_async.py @@ -8,6 +8,7 @@ from collections.abc import AsyncGenerator from collections.abc import Awaitable from collections.abc import Coroutine +import inspect from typing import Any from typing import cast @@ -179,7 +180,10 @@ async def coro() -> None: awaitable = coro() with pytest.raises(RuntimeError, match="outside of async context"): submitter.require_await(awaitable) - awaitable.close() + # require_await takes ownership either way: an awaitable it declines to + # await is closed, not left to warn about never being awaited when + # collected. + assert inspect.getcoroutinestate(awaitable) == inspect.CORO_CLOSED def test_maybe_submit_outside_context_returns_awaitable() -> None: