Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This package provides a subclass of
which

- allows async functions, context managers, and async context managers to be hooks
- supports `firstresult=True` for async hooks
- and accepts plugin factories in addition to plugin instances for registration.

---
Expand All @@ -30,6 +31,8 @@ which
- [Create a plugin manager and register plugins](#create-a-plugin-manager-and-register-plugins)
- [Call hooks](#call-hooks)
- [Async function](#async-function)
- [firstresult=True](#firstresulttrue)
- [Exceptions](#exceptions)
- [Context manager](#context-manager)
- [Async context manager](#async-context-manager)
- [Links](#links)
Expand Down Expand Up @@ -209,6 +212,49 @@ inside Plugin_1.afunc()

```

##### firstresult=True

When the hookspec is defined with `firstresult=True`, `ahook` returns the
first non-`None` result instead of a list. Implementations are awaited
sequentially in pluggy's standard execution order (reverse registration order,
with `tryfirst` and `trylast` modifiers respected) and the chain stops at the
first non-`None` return value. This is unlike the default behavior, where all
implementations are gathered concurrently.

```python
>>> class SpecFR:
... @hookspec(firstresult=True)
... async def afunc_first(self, arg1, arg2):
... pass

>>> class Plugin_FR:
... @hookimpl
... async def afunc_first(self, arg1, arg2):
... return arg1 + arg2

>>> pm_fr = pluggy.PluginManager('project')
>>> pm_fr.add_hookspecs(SpecFR)
>>> _ = pm_fr.register(Plugin_FR())

>>> async def call_afunc_first():
... result = await pm_fr.ahook.afunc_first(arg1=1, arg2=2)
... print(result)

>>> asyncio.run(call_afunc_first())
3

```

##### Exceptions

With `pm.ahook`, an exception from one implementation propagates to the
caller, unless a wrapper handles it, and the other results are lost. On a
broadcast hook without wrappers, the other implementations keep running,
unobserved, and any exception they raise later is never reported. On a
broadcast hook with a wrapper, every implementation finishes first and the
first exception in hook order is thrown into the wrappers. A `firstresult=True`
hook runs its implementations one at a time and stops at the first exception.

#### Context manager

```python
Expand Down
Loading