Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to nest context managers in the reverse order #4

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add awith_reverse
TaiSakuma committed Nov 17, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 7b9478ebc2dfb78abcc4d6f926237944eda8d899
16 changes: 13 additions & 3 deletions src/apluggy/_wrap/awith.py
Original file line number Diff line number Diff line change
@@ -12,16 +12,26 @@ def __init__(self, pm: PluginManager_) -> None:

def __getattr__(self, name: str) -> Callable[..., AsyncContextManager]:
hook: HookCaller = getattr(self.pm.hook, name)
call = _Call(hook)
return call
return _Call(hook)


class AWithReverse:
def __init__(self, pm: PluginManager_) -> None:
self.pm = pm

def __getattr__(self, name: str) -> Callable[..., AsyncContextManager]:
hook: HookCaller = getattr(self.pm.hook, name)
return _Call(hook, reverse=True)


def _Call(
hook: Callable[..., list[AsyncContextManager]]
hook: Callable[..., list[AsyncContextManager]], reverse: bool = False
) -> Callable[..., AsyncContextManager]:
@contextlib.asynccontextmanager
async def call(*args: Any, **kwargs: Any) -> AsyncIterator[list]:
ctxs = hook(*args, **kwargs)
if reverse:
ctxs = list(reversed(ctxs))
async with contextlib.AsyncExitStack() as stack:
yields = [await stack.enter_async_context(ctx) for ctx in ctxs]

3 changes: 2 additions & 1 deletion src/apluggy/_wrap/manager.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
from pluggy._hooks import _Plugin

from .ahook import AHook
from .awith import AWith
from .awith import AWith, AWithReverse
from .with_ import With, WithReverse


@@ -118,6 +118,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.with_ = With(self)
self.with_reverse = WithReverse(self)
self.awith = AWith(self)
self.awith_reverse = AWithReverse(self)

def register(
self, plugin: _Plugin | Callable[[], _Plugin], name: Optional[str] = None
5 changes: 5 additions & 0 deletions tests/plugins/test_call.py
Original file line number Diff line number Diff line change
@@ -51,6 +51,11 @@ async def test_awith(pm: PluginManager):
assert r == [-1, -1, 3]


async def test_awith_reverse(pm: PluginManager):
async with pm.awith_reverse.acontext(arg1=1, arg2=2) as r:
assert r == [3, -1, -1]


def test_name(pm: PluginManager):
id_ = id(pm.list_name_plugin()[1][1]) # the object id of the 2nd plugin