Skip to content

Commit 428ab80

Browse files
author
Tyler Goodlet
committed
Document LIFO hook ordering by plugin registration
Resolves #64
1 parent 84fb9ea commit 428ab80

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

docs/index.rst

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ then the *hookimpl* should be marked with the ``"optionalhook"`` option:
152152
153153
Call time order
154154
^^^^^^^^^^^^^^^
155-
A *hookimpl* can influence its call-time invocation position.
156-
If marked with a ``"tryfirst"`` or ``"trylast"`` option it will be
157-
executed *first* or *last* respectively in the hook call loop:
155+
By default hooks are :ref:`called <calling>` in LIFO registered order, however,
156+
a *hookimpl* can influence its call-time invocation position using special
157+
attributes. If marked with a ``"tryfirst"`` or ``"trylast"`` option it
158+
will be executed *first* or *last* respectively in the hook call loop:
158159

159160
.. code-block:: python
160161
@@ -477,6 +478,8 @@ You can retrieve the *options* applied to a particular
477478
http://doc.pytest.org/en/latest/writing_plugins.html#setuptools-entry-points
478479

479480

481+
.. _calling:
482+
480483
Calling Hooks
481484
*************
482485
The core functionality of ``pluggy`` enables an extension provider
@@ -487,7 +490,7 @@ a :py:class:`pluggy._HookCaller` which in turn *loops* through the
487490
``1:N`` registered *hookimpls* and calls them in sequence.
488491

489492
Every :py:class:`pluggy.PluginManager` has a ``hook`` attribute
490-
which is an instance of a :py:class:`pluggy._HookRelay`.
493+
which is an instance of this :py:class:`pluggy._HookRelay`.
491494
The ``_HookRelay`` itself contains references (by hook name) to each
492495
registered *hookimpl*'s ``_HookCaller`` instance.
493496

@@ -510,6 +513,40 @@ More practically you call a *hook* like so:
510513
511514
Note that you **must** call hooks using keyword `arguments`_ syntax!
512515

516+
Hook implementations are called in LIFO registered order: *the last
517+
registered plugin's hooks are called first*. As an example, the below
518+
assertion should not error:
519+
520+
.. code-block:: python
521+
522+
from pluggy import PluginManager, HookimplMarker
523+
524+
hookimpl = HookimplMarker('myproject')
525+
526+
class Plugin1(object):
527+
def myhook(self, args):
528+
"""Default implementation.
529+
"""
530+
return 1
531+
532+
class Plugin2(object):
533+
def myhook(self, args):
534+
"""Default implementation.
535+
"""
536+
return 2
537+
538+
class Plugin3(object):
539+
def myhook(self, args):
540+
"""Default implementation.
541+
"""
542+
return 3
543+
544+
pm = PluginManager('myproject')
545+
pm.register(Plugin1())
546+
pm.register(Plugin2())
547+
pm.register(Plugin3())
548+
549+
assert pm.hook.myhook(args=()) == [3, 2, 1]
513550
514551
Collecting results
515552
------------------

0 commit comments

Comments
 (0)