Skip to content

Commit

Permalink
Note on try-finally for wrappers in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnikulin committed Feb 8, 2022
1 parent b2edd22 commit 9532856
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,19 @@ the final result(s) returned back to the caller using the
:py:meth:`~pluggy._callers._Result.force_result` or
:py:meth:`~pluggy._callers._Result.get_result` methods.

If a wrapper is intended for setup and tear down of other stuff,
do not forget to surround ``yield`` and ``outcome.get_result()``
with ``try`` and ``finally`` exactly as it is recommended for
:py:func:`@contextlib.contextmanager <python:contextlib.contextmanager>`.
``outcome.get_result()`` could raise an exception if some other hook failed.
For future compatibility it is better to assume that ``yield`` could
throw as well.

.. note::
Hook wrappers can **not** return results (as per generator function
semantics); they can only modify them using the ``_Result`` API.
However an exception following ``yield`` implicitly replaces result
for the outer wrappers if there are any of them.

Also see the :ref:`pytest:hookwrapper` section in the ``pytest`` docs.

Expand Down

0 comments on commit 9532856

Please sign in to comment.