Skip to content

Commit 8040cfd

Browse files
authored
Merge pull request #9401 from bluetech/doc-hooks-ref
doc: add a `hook` crossref type
2 parents ab8c984 + 0e69c62 commit 8040cfd

File tree

7 files changed

+90
-37
lines changed

7 files changed

+90
-37
lines changed

doc/en/changelog.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Deprecations
156156
See :ref:`the deprecation note <diamond-inheritance-deprecated>` for full details.
157157

158158

159-
- `#8592 <https://github.com/pytest-dev/pytest/issues/8592>`_: :func:`pytest_cmdline_preparse <_pytest.hookspec.pytest_cmdline_preparse>` has been officially deprecated. It will be removed in a future release. Use :func:`pytest_load_initial_conftests <_pytest.hookspec.pytest_load_initial_conftests>` instead.
159+
- `#8592 <https://github.com/pytest-dev/pytest/issues/8592>`_: :hook:`pytest_cmdline_preparse` has been officially deprecated. It will be removed in a future release. Use :hook:`pytest_load_initial_conftests` instead.
160160

161161
See :ref:`the deprecation note <cmdline-preparse-deprecated>` for full details.
162162

@@ -203,11 +203,11 @@ Features
203203
- ``pytest.Mark`` for :class:`marks <pytest.Mark>`.
204204
- ``pytest.MarkDecorator`` for :class:`mark decorators <pytest.MarkDecorator>`.
205205
- ``pytest.MarkGenerator`` for the :class:`pytest.mark <pytest.MarkGenerator>` singleton.
206-
- ``pytest.Metafunc`` for the :class:`metafunc <pytest.MarkGenerator>` argument to the :func:`pytest_generate_tests <pytest.hookspec.pytest_generate_tests>` hook.
206+
- ``pytest.Metafunc`` for the :class:`metafunc <pytest.MarkGenerator>` argument to the :hook:`pytest_generate_tests` hook.
207207
- ``pytest.CallInfo`` for the :class:`CallInfo <pytest.CallInfo>` type passed to various hooks.
208208
- ``pytest.PytestPluginManager`` for :class:`PytestPluginManager <pytest.PytestPluginManager>`.
209209
- ``pytest.ExceptionInfo`` for the :class:`ExceptionInfo <pytest.ExceptionInfo>` type returned from :func:`pytest.raises` and passed to various hooks.
210-
- ``pytest.Parser`` for the :class:`Parser <pytest.Parser>` type passed to the :func:`pytest_addoption <pytest.hookspec.pytest_addoption>` hook.
210+
- ``pytest.Parser`` for the :class:`Parser <pytest.Parser>` type passed to the :hook:`pytest_addoption` hook.
211211
- ``pytest.OptionGroup`` for the :class:`OptionGroup <pytest.OptionGroup>` type returned from the :func:`parser.addgroup <pytest.Parser.getgroup>` method.
212212
- ``pytest.HookRecorder`` for the :class:`HookRecorder <pytest.HookRecorder>` type returned from :class:`~pytest.Pytester`.
213213
- ``pytest.RecordedHookCall`` for the :class:`RecordedHookCall <pytest.HookRecorder>` type returned from :class:`~pytest.HookRecorder`.
@@ -228,11 +228,11 @@ Features
228228

229229
- `#8144 <https://github.com/pytest-dev/pytest/issues/8144>`_: The following hooks now receive an additional ``pathlib.Path`` argument, equivalent to an existing ``py.path.local`` argument:
230230

231-
- :func:`pytest_ignore_collect <_pytest.hookspec.pytest_ignore_collect>` - The ``collection_path`` parameter (equivalent to existing ``path`` parameter).
232-
- :func:`pytest_collect_file <_pytest.hookspec.pytest_collect_file>` - The ``file_path`` parameter (equivalent to existing ``path`` parameter).
233-
- :func:`pytest_pycollect_makemodule <_pytest.hookspec.pytest_pycollect_makemodule>` - The ``module_path`` parameter (equivalent to existing ``path`` parameter).
234-
- :func:`pytest_report_header <_pytest.hookspec.pytest_report_header>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
235-
- :func:`pytest_report_collectionfinish <_pytest.hookspec.pytest_report_collectionfinish>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
231+
- :hook:`pytest_ignore_collect` - The ``collection_path`` parameter (equivalent to existing ``path`` parameter).
232+
- :hook:`pytest_collect_file` - The ``file_path`` parameter (equivalent to existing ``path`` parameter).
233+
- :hook:`pytest_pycollect_makemodule` - The ``module_path`` parameter (equivalent to existing ``path`` parameter).
234+
- :hook:`pytest_report_header` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
235+
- :hook:`pytest_report_collectionfinish` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
236236

237237
.. note::
238238
The name of the :class:`~_pytest.nodes.Node` arguments and attributes (the
@@ -491,7 +491,7 @@ Trivial/Internal Changes
491491
- `#8913 <https://github.com/pytest-dev/pytest/issues/8913>`_: The private ``CallSpec2._arg2scopenum`` attribute has been removed after an internal refactoring.
492492

493493

494-
- `#8967 <https://github.com/pytest-dev/pytest/issues/8967>`_: :func:`pytest_assertion_pass <_pytest.hookspec.pytest_assertion_pass>` is no longer considered experimental and
494+
- `#8967 <https://github.com/pytest-dev/pytest/issues/8967>`_: :hook:`pytest_assertion_pass` is no longer considered experimental and
495495
future changes to it will be considered more carefully.
496496

497497

@@ -853,8 +853,8 @@ Deprecations
853853
if you use this and want a replacement.
854854

855855

856-
- :issue:`7255`: The :func:`pytest_warning_captured <_pytest.hookspec.pytest_warning_captured>` hook is deprecated in favor
857-
of :func:`pytest_warning_recorded <_pytest.hookspec.pytest_warning_recorded>`, and will be removed in a future version.
856+
- :issue:`7255`: The :hook:`pytest_warning_captured` hook is deprecated in favor
857+
of :hook:`pytest_warning_recorded`, and will be removed in a future version.
858858

859859

860860
- :issue:`7648`: The ``gethookproxy()`` and ``isinitpath()`` methods of ``FSCollector`` and ``Package`` are deprecated;
@@ -962,7 +962,7 @@ Trivial/Internal Changes
962962
process, pytest now ignores builtin attributes (like ``__class__``,
963963
``__delattr__`` and ``__new__``) without consulting the :confval:`python_classes` and
964964
:confval:`python_functions` configuration options and without passing them to plugins
965-
using the :func:`pytest_pycollect_makeitem <_pytest.hookspec.pytest_pycollect_makeitem>` hook.
965+
using the :hook:`pytest_pycollect_makeitem` hook.
966966

967967

968968
pytest 6.0.2 (2020-09-04)
@@ -1751,7 +1751,7 @@ Bug Fixes
17511751
- :issue:`6646`: Assertion rewriting hooks are (re)stored for the current item, which fixes them being still used after e.g. pytester's :func:`testdir.runpytest <_pytest.pytester.Testdir.runpytest>` etc.
17521752

17531753

1754-
- :issue:`6660`: :py:func:`pytest.exit` is handled when emitted from the :func:`pytest_sessionfinish <_pytest.hookspec.pytest_sessionfinish>` hook. This includes quitting from a debugger.
1754+
- :issue:`6660`: :py:func:`pytest.exit` is handled when emitted from the :hook:`pytest_sessionfinish` hook. This includes quitting from a debugger.
17551755

17561756

17571757
- :issue:`6752`: When :py:func:`pytest.raises` is used as a function (as opposed to a context manager),
@@ -1863,7 +1863,7 @@ Improvements
18631863
- :issue:`6231`: Improve check for misspelling of :ref:`pytest.mark.parametrize ref`.
18641864

18651865

1866-
- :issue:`6257`: Handle :py:func:`pytest.exit` being used via :py:func:`~_pytest.hookspec.pytest_internalerror`, e.g. when quitting pdb from post mortem.
1866+
- :issue:`6257`: Handle :func:`pytest.exit` being used via :hook:`pytest_internalerror`, e.g. when quitting pdb from post mortem.
18671867

18681868

18691869

@@ -2497,7 +2497,7 @@ Deprecations
24972497
Features
24982498
--------
24992499

2500-
- :issue:`3457`: New :func:`~_pytest.hookspec.pytest_assertion_pass`
2500+
- :issue:`3457`: New :hook:`pytest_assertion_pass`
25012501
hook, called with context information when an assertion *passes*.
25022502

25032503
This hook is still **experimental** so use it with caution.
@@ -5017,9 +5017,9 @@ Features
50175017
- Console output falls back to "classic" mode when capturing is disabled (``-s``),
50185018
otherwise the output gets garbled to the point of being useless. (:issue:`3038`)
50195019

5020-
- New :func:`~_pytest.hookspec.pytest_runtest_logfinish`
5020+
- New :hook:`pytest_runtest_logfinish`
50215021
hook which is called when a test item has finished executing, analogous to
5022-
:func:`~_pytest.hookspec.pytest_runtest_logstart`.
5022+
:hook:`pytest_runtest_logstart`.
50235023
(:issue:`3101`)
50245024

50255025
- Improve performance when collecting tests using many fixtures. (:issue:`3107`)

doc/en/conf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ def setup(app: "sphinx.application.Sphinx") -> None:
445445
indextemplate="pair: %s; global variable interpreted by pytest",
446446
)
447447

448+
app.add_crossref_type(
449+
directivename="hook",
450+
rolename="hook",
451+
objname="pytest hook",
452+
indextemplate="pair: %s; hook",
453+
)
454+
448455
configure_logging(app)
449456

450457
# Make Sphinx mark classes with "final" when decorated with @final.

doc/en/deprecations.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ no matter what argument was used in the constructor. We expect to deprecate the
8181

8282
In order to support the transition from ``py.path.local`` to :mod:`pathlib`, the following hooks now receive additional arguments:
8383

84-
* :func:`pytest_ignore_collect(collection_path: pathlib.Path) <_pytest.hookspec.pytest_ignore_collect>` as equivalent to ``path``
85-
* :func:`pytest_collect_file(file_path: pathlib.Path) <_pytest.hookspec.pytest_collect_file>` as equivalent to ``path``
86-
* :func:`pytest_pycollect_makemodule(module_path: pathlib.Path) <_pytest.hookspec.pytest_pycollect_makemodule>` as equivalent to ``path``
87-
* :func:`pytest_report_header(start_path: pathlib.Path) <_pytest.hookspec.pytest_report_header>` as equivalent to ``startdir``
88-
* :func:`pytest_report_collectionfinish(start_path: pathlib.Path) <_pytest.hookspec.pytest_report_collectionfinish>` as equivalent to ``startdir``
84+
* :hook:`pytest_ignore_collect(collection_path: pathlib.Path) <pytest_ignore_collect>` as equivalent to ``path``
85+
* :hook:`pytest_collect_file(file_path: pathlib.Path) <pytest_collect_file>` as equivalent to ``path``
86+
* :hook:`pytest_pycollect_makemodule(module_path: pathlib.Path) <pytest_pycollect_makemodule>` as equivalent to ``path``
87+
* :hook:`pytest_report_header(start_path: pathlib.Path) <pytest_report_header>` as equivalent to ``startdir``
88+
* :hook:`pytest_report_collectionfinish(start_path: pathlib.Path) <pytest_report_collectionfinish>` as equivalent to ``startdir``
8989

9090
The accompanying ``py.path.local`` based paths have been deprecated: plugins which manually invoke those hooks should only pass the new ``pathlib.Path`` arguments, and users should change their hook implementations to use the new ``pathlib.Path`` arguments.
9191

@@ -157,8 +157,8 @@ Implementing the ``pytest_cmdline_preparse`` hook
157157

158158
.. deprecated:: 7.0
159159

160-
Implementing the :func:`pytest_cmdline_preparse <_pytest.hookspec.pytest_cmdline_preparse>` hook has been officially deprecated.
161-
Implement the :func:`pytest_load_initial_conftests <_pytest.hookspec.pytest_load_initial_conftests>` hook instead.
160+
Implementing the :hook:`pytest_cmdline_preparse` hook has been officially deprecated.
161+
Implement the :hook:`pytest_load_initial_conftests` hook instead.
162162

163163
.. code-block:: python
164164
@@ -331,8 +331,8 @@ at some point, depending on the plans for the plugins and number of users using
331331

332332
.. versionremoved:: 6.0
333333

334-
The ``pytest_collect_directory`` has not worked properly for years (it was called
335-
but the results were ignored). Users may consider using :func:`pytest_collection_modifyitems <_pytest.hookspec.pytest_collection_modifyitems>` instead.
334+
The ``pytest_collect_directory`` hook has not worked properly for years (it was called
335+
but the results were ignored). Users may consider using :hook:`pytest_collection_modifyitems` instead.
336336

337337
TerminalReporter.writer
338338
~~~~~~~~~~~~~~~~~~~~~~~

doc/en/reference/reference.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,13 @@ Bootstrapping hooks
671671

672672
Bootstrapping hooks called for plugins registered early enough (internal and setuptools plugins).
673673

674+
.. hook:: pytest_load_initial_conftests
674675
.. autofunction:: pytest_load_initial_conftests
676+
.. hook:: pytest_cmdline_preparse
675677
.. autofunction:: pytest_cmdline_preparse
678+
.. hook:: pytest_cmdline_parse
676679
.. autofunction:: pytest_cmdline_parse
680+
.. hook:: pytest_cmdline_main
677681
.. autofunction:: pytest_cmdline_main
678682

679683
.. _`initialization-hooks`:
@@ -683,35 +687,50 @@ Initialization hooks
683687

684688
Initialization hooks called for plugins and ``conftest.py`` files.
685689

690+
.. hook:: pytest_addoption
686691
.. autofunction:: pytest_addoption
692+
.. hook:: pytest_addhooks
687693
.. autofunction:: pytest_addhooks
694+
.. hook:: pytest_configure
688695
.. autofunction:: pytest_configure
696+
.. hook:: pytest_unconfigure
689697
.. autofunction:: pytest_unconfigure
698+
.. hook:: pytest_sessionstart
690699
.. autofunction:: pytest_sessionstart
700+
.. hook:: pytest_sessionfinish
691701
.. autofunction:: pytest_sessionfinish
692702

703+
.. hook:: pytest_plugin_registered
693704
.. autofunction:: pytest_plugin_registered
694705

695706
Collection hooks
696707
~~~~~~~~~~~~~~~~
697708

698709
``pytest`` calls the following hooks for collecting files and directories:
699710

711+
.. hook:: pytest_collection
700712
.. autofunction:: pytest_collection
713+
.. hook:: pytest_ignore_collect
701714
.. autofunction:: pytest_ignore_collect
715+
.. hook:: pytest_collect_file
702716
.. autofunction:: pytest_collect_file
717+
.. hook:: pytest_pycollect_makemodule
703718
.. autofunction:: pytest_pycollect_makemodule
704719

705720
For influencing the collection of objects in Python modules
706721
you can use the following hook:
707722

723+
.. hook:: pytest_pycollect_makeitem
708724
.. autofunction:: pytest_pycollect_makeitem
725+
.. hook:: pytest_generate_tests
709726
.. autofunction:: pytest_generate_tests
727+
.. hook:: pytest_make_parametrize_id
710728
.. autofunction:: pytest_make_parametrize_id
711729

712730
After collection is complete, you can modify the order of
713731
items, delete or otherwise amend the test items:
714732

733+
.. hook:: pytest_collection_modifyitems
715734
.. autofunction:: pytest_collection_modifyitems
716735

717736
.. note::
@@ -725,13 +744,21 @@ Test running (runtest) hooks
725744

726745
All runtest related hooks receive a :py:class:`pytest.Item <pytest.Item>` object.
727746

747+
.. hook:: pytest_runtestloop
728748
.. autofunction:: pytest_runtestloop
749+
.. hook:: pytest_runtest_protocol
729750
.. autofunction:: pytest_runtest_protocol
751+
.. hook:: pytest_runtest_logstart
730752
.. autofunction:: pytest_runtest_logstart
753+
.. hook:: pytest_runtest_logfinish
731754
.. autofunction:: pytest_runtest_logfinish
755+
.. hook:: pytest_runtest_setup
732756
.. autofunction:: pytest_runtest_setup
757+
.. hook:: pytest_runtest_call
733758
.. autofunction:: pytest_runtest_call
759+
.. hook:: pytest_runtest_teardown
734760
.. autofunction:: pytest_runtest_teardown
761+
.. hook:: pytest_runtest_makereport
735762
.. autofunction:: pytest_runtest_makereport
736763

737764
For deeper understanding you may look at the default implementation of
@@ -740,33 +767,49 @@ in ``_pytest.pdb`` which interacts with ``_pytest.capture``
740767
and its input/output capturing in order to immediately drop
741768
into interactive debugging when a test failure occurs.
742769

770+
.. hook:: pytest_pyfunc_call
743771
.. autofunction:: pytest_pyfunc_call
744772

745773
Reporting hooks
746774
~~~~~~~~~~~~~~~
747775

748776
Session related reporting hooks:
749777

778+
.. hook:: pytest_collectstart
750779
.. autofunction:: pytest_collectstart
780+
.. hook:: pytest_make_collect_report
751781
.. autofunction:: pytest_make_collect_report
782+
.. hook:: pytest_itemcollected
752783
.. autofunction:: pytest_itemcollected
784+
.. hook:: pytest_collectreport
753785
.. autofunction:: pytest_collectreport
786+
.. hook:: pytest_deselected
754787
.. autofunction:: pytest_deselected
788+
.. hook:: pytest_report_header
755789
.. autofunction:: pytest_report_header
790+
.. hook:: pytest_report_collectionfinish
756791
.. autofunction:: pytest_report_collectionfinish
792+
.. hook:: pytest_report_teststatus
757793
.. autofunction:: pytest_report_teststatus
794+
.. hook:: pytest_terminal_summary
758795
.. autofunction:: pytest_terminal_summary
796+
.. hook:: pytest_fixture_setup
759797
.. autofunction:: pytest_fixture_setup
798+
.. hook:: pytest_fixture_post_finalizer
760799
.. autofunction:: pytest_fixture_post_finalizer
800+
.. hook:: pytest_warning_recorded
761801
.. autofunction:: pytest_warning_recorded
762802

763803
Central hook for reporting about test execution:
764804

805+
.. hook:: pytest_runtest_logreport
765806
.. autofunction:: pytest_runtest_logreport
766807

767808
Assertion related hooks:
768809

810+
.. hook:: pytest_assertrepr_compare
769811
.. autofunction:: pytest_assertrepr_compare
812+
.. hook:: pytest_assertion_pass
770813
.. autofunction:: pytest_assertion_pass
771814

772815

@@ -776,9 +819,13 @@ Debugging/Interaction hooks
776819
There are few hooks which can be used for special
777820
reporting or interaction with exceptions:
778821

822+
.. hook:: pytest_internalerror
779823
.. autofunction:: pytest_internalerror
824+
.. hook:: pytest_keyboard_interrupt
780825
.. autofunction:: pytest_keyboard_interrupt
826+
.. hook:: pytest_exception_interact
781827
.. autofunction:: pytest_exception_interact
828+
.. hook:: pytest_enter_pdb
782829
.. autofunction:: pytest_enter_pdb
783830

784831

src/_pytest/hookspec.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def pytest_cmdline_preparse(config: "Config", args: List[str]) -> None:
162162
"""(**Deprecated**) modify command line arguments before option parsing.
163163
164164
This hook is considered deprecated and will be removed in a future pytest version. Consider
165-
using :func:`pytest_load_initial_conftests` instead.
165+
using :hook:`pytest_load_initial_conftests` instead.
166166
167167
.. note::
168168
This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
@@ -466,7 +466,7 @@ def pytest_runtest_logstart(
466466
) -> None:
467467
"""Called at the start of running the runtest protocol for a single item.
468468
469-
See :func:`pytest_runtest_protocol` for a description of the runtest protocol.
469+
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
470470
471471
:param str nodeid: Full node ID of the item.
472472
:param location: A tuple of ``(filename, lineno, testname)``.
@@ -478,7 +478,7 @@ def pytest_runtest_logfinish(
478478
) -> None:
479479
"""Called at the end of running the runtest protocol for a single item.
480480
481-
See :func:`pytest_runtest_protocol` for a description of the runtest protocol.
481+
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
482482
483483
:param str nodeid: Full node ID of the item.
484484
:param location: A tuple of ``(filename, lineno, testname)``.
@@ -525,7 +525,7 @@ def pytest_runtest_makereport(
525525
"""Called to create a :class:`~pytest.TestReport` for each of
526526
the setup, call and teardown runtest phases of a test item.
527527
528-
See :func:`pytest_runtest_protocol` for a description of the runtest protocol.
528+
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
529529
530530
:param call: The :class:`~pytest.CallInfo` for the phase.
531531
@@ -537,7 +537,7 @@ def pytest_runtest_logreport(report: "TestReport") -> None:
537537
"""Process the :class:`~pytest.TestReport` produced for each
538538
of the setup, call and teardown runtest phases of an item.
539539
540-
See :func:`pytest_runtest_protocol` for a description of the runtest protocol.
540+
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
541541
"""
542542

543543

@@ -556,7 +556,7 @@ def pytest_report_from_serializable(
556556
data: Dict[str, Any],
557557
) -> Optional[Union["CollectReport", "TestReport"]]:
558558
"""Restore a report object previously serialized with
559-
:func:`pytest_report_to_serializable`."""
559+
:hook:`pytest_report_to_serializable`."""
560560

561561

562562
# -------------------------------------------------------------------------
@@ -859,10 +859,10 @@ def pytest_exception_interact(
859859
"""Called when an exception was raised which can potentially be
860860
interactively handled.
861861
862-
May be called during collection (see :py:func:`pytest_make_collect_report`),
862+
May be called during collection (see :hook:`pytest_make_collect_report`),
863863
in which case ``report`` is a :class:`CollectReport`.
864864
865-
May be called during runtest of an item (see :py:func:`pytest_runtest_protocol`),
865+
May be called during runtest of an item (see :hook:`pytest_runtest_protocol`),
866866
in which case ``report`` is a :class:`TestReport`.
867867
868868
This hook is not called if the exception that was raised is an internal

src/_pytest/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,7 @@ def perform_collect(
605605
) -> Sequence[Union[nodes.Item, nodes.Collector]]:
606606
"""Perform the collection phase for this session.
607607
608-
This is called by the default
609-
:func:`pytest_collection <_pytest.hookspec.pytest_collection>` hook
608+
This is called by the default :hook:`pytest_collection` hook
610609
implementation; see the documentation of this hook for more details.
611610
For testing purposes, it may also be called directly on a fresh
612611
``Session``.

src/_pytest/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ def id(self) -> str:
985985

986986
@final
987987
class Metafunc:
988-
"""Objects passed to the :func:`pytest_generate_tests <_pytest.hookspec.pytest_generate_tests>` hook.
988+
"""Objects passed to the :hook:`pytest_generate_tests` hook.
989989
990990
They help to inspect a test function and to generate tests according to
991991
test configuration or values specified in the class or module where a

0 commit comments

Comments
 (0)