Skip to content

Commit a566b78

Browse files
[7.4.x] reference: improve the node types docs a bit (#11181)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 511adf8 commit a566b78

File tree

4 files changed

+102
-77
lines changed

4 files changed

+102
-77
lines changed

doc/en/reference/reference.rst

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -783,25 +783,17 @@ reporting or interaction with exceptions:
783783
.. autofunction:: pytest_leave_pdb
784784

785785

786-
Objects
787-
-------
788-
789-
Full reference to objects accessible from :ref:`fixtures <fixture>` or :ref:`hooks <hook-reference>`.
786+
Collection tree objects
787+
-----------------------
790788

789+
These are the collector and item classes (collectively called "nodes") which
790+
make up the collection tree.
791791

792-
CallInfo
793-
~~~~~~~~
794-
795-
.. autoclass:: pytest.CallInfo()
796-
:members:
797-
798-
799-
Class
800-
~~~~~
792+
Node
793+
~~~~
801794

802-
.. autoclass:: pytest.Class()
795+
.. autoclass:: _pytest.nodes.Node()
803796
:members:
804-
:show-inheritance:
805797

806798
Collector
807799
~~~~~~~~~
@@ -810,52 +802,52 @@ Collector
810802
:members:
811803
:show-inheritance:
812804

813-
CollectReport
814-
~~~~~~~~~~~~~
805+
Item
806+
~~~~
815807

816-
.. autoclass:: pytest.CollectReport()
808+
.. autoclass:: pytest.Item()
817809
:members:
818810
:show-inheritance:
819-
:inherited-members:
820811

821-
Config
822-
~~~~~~
812+
File
813+
~~~~
823814

824-
.. autoclass:: pytest.Config()
815+
.. autoclass:: pytest.File()
825816
:members:
817+
:show-inheritance:
826818

827-
ExceptionInfo
828-
~~~~~~~~~~~~~
819+
FSCollector
820+
~~~~~~~~~~~
829821

830-
.. autoclass:: pytest.ExceptionInfo()
822+
.. autoclass:: _pytest.nodes.FSCollector()
831823
:members:
824+
:show-inheritance:
832825

826+
Session
827+
~~~~~~~
833828

834-
ExitCode
835-
~~~~~~~~
836-
837-
.. autoclass:: pytest.ExitCode
829+
.. autoclass:: pytest.Session()
838830
:members:
831+
:show-inheritance:
839832

840-
File
841-
~~~~
833+
Package
834+
~~~~~~~
842835

843-
.. autoclass:: pytest.File()
836+
.. autoclass:: pytest.Package()
844837
:members:
845838
:show-inheritance:
846839

840+
Module
841+
~~~~~~
847842

848-
FixtureDef
849-
~~~~~~~~~~
850-
851-
.. autoclass:: _pytest.fixtures.FixtureDef()
843+
.. autoclass:: pytest.Module()
852844
:members:
853845
:show-inheritance:
854846

855-
FSCollector
856-
~~~~~~~~~~~
847+
Class
848+
~~~~~
857849

858-
.. autoclass:: _pytest.nodes.FSCollector()
850+
.. autoclass:: pytest.Class()
859851
:members:
860852
:show-inheritance:
861853

@@ -873,10 +865,52 @@ FunctionDefinition
873865
:members:
874866
:show-inheritance:
875867

876-
Item
877-
~~~~
878868

879-
.. autoclass:: pytest.Item()
869+
Objects
870+
-------
871+
872+
Objects accessible from :ref:`fixtures <fixture>` or :ref:`hooks <hook-reference>`
873+
or importable from ``pytest``.
874+
875+
876+
CallInfo
877+
~~~~~~~~
878+
879+
.. autoclass:: pytest.CallInfo()
880+
:members:
881+
882+
CollectReport
883+
~~~~~~~~~~~~~
884+
885+
.. autoclass:: pytest.CollectReport()
886+
:members:
887+
:show-inheritance:
888+
:inherited-members:
889+
890+
Config
891+
~~~~~~
892+
893+
.. autoclass:: pytest.Config()
894+
:members:
895+
896+
ExceptionInfo
897+
~~~~~~~~~~~~~
898+
899+
.. autoclass:: pytest.ExceptionInfo()
900+
:members:
901+
902+
903+
ExitCode
904+
~~~~~~~~
905+
906+
.. autoclass:: pytest.ExitCode
907+
:members:
908+
909+
910+
FixtureDef
911+
~~~~~~~~~~
912+
913+
.. autoclass:: _pytest.fixtures.FixtureDef()
880914
:members:
881915
:show-inheritance:
882916

@@ -907,19 +941,6 @@ Metafunc
907941
.. autoclass:: pytest.Metafunc()
908942
:members:
909943

910-
Module
911-
~~~~~~
912-
913-
.. autoclass:: pytest.Module()
914-
:members:
915-
:show-inheritance:
916-
917-
Node
918-
~~~~
919-
920-
.. autoclass:: _pytest.nodes.Node()
921-
:members:
922-
923944
Parser
924945
~~~~~~
925946

@@ -941,13 +962,6 @@ PytestPluginManager
941962
:inherited-members:
942963
:show-inheritance:
943964

944-
Session
945-
~~~~~~~
946-
947-
.. autoclass:: pytest.Session()
948-
:members:
949-
:show-inheritance:
950-
951965
TestReport
952966
~~~~~~~~~~
953967

src/_pytest/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ def __missing__(self, path: Path) -> str:
462462

463463
@final
464464
class Session(nodes.FSCollector):
465+
"""The root of the collection tree.
466+
467+
``Session`` collects the initial paths given as arguments to pytest.
468+
"""
469+
465470
Interrupted = Interrupted
466471
Failed = Failed
467472
# Set on the session by runner.pytest_sessionstart.

src/_pytest/nodes.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ def _create(self, *k, **kw):
157157

158158

159159
class Node(metaclass=NodeMeta):
160-
"""Base class for Collector and Item, the components of the test
161-
collection tree.
160+
r"""Base class of :class:`Collector` and :class:`Item`, the components of
161+
the test collection tree.
162162
163-
Collector subclasses have children; Items are leaf nodes.
163+
``Collector``\'s are the internal nodes of the tree, and ``Item``\'s are the
164+
leaf nodes.
164165
"""
165166

166167
# Implemented in the legacypath plugin.
@@ -525,15 +526,17 @@ def get_fslocation_from_item(node: "Node") -> Tuple[Union[str, Path], Optional[i
525526

526527

527528
class Collector(Node):
528-
"""Collector instances create children through collect() and thus
529-
iteratively build a tree."""
529+
"""Base class of all collectors.
530+
531+
Collector create children through `collect()` and thus iteratively build
532+
the collection tree.
533+
"""
530534

531535
class CollectError(Exception):
532536
"""An error during collection, contains a custom message."""
533537

534538
def collect(self) -> Iterable[Union["Item", "Collector"]]:
535-
"""Return a list of children (items and collectors) for this
536-
collection node."""
539+
"""Collect children (items and collectors) for this collector."""
537540
raise NotImplementedError("abstract")
538541

539542
# TODO: This omits the style= parameter which breaks Liskov Substitution.
@@ -577,6 +580,8 @@ def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[
577580

578581

579582
class FSCollector(Collector):
583+
"""Base class for filesystem collectors."""
584+
580585
def __init__(
581586
self,
582587
fspath: Optional[LEGACY_PATH] = None,
@@ -660,7 +665,7 @@ class File(FSCollector):
660665

661666

662667
class Item(Node):
663-
"""A basic test invocation item.
668+
"""Base class of all test invocation items.
664669
665670
Note that for a single function there might be multiple test invocation items.
666671
"""

src/_pytest/python.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
522522

523523

524524
class Module(nodes.File, PyCollector):
525-
"""Collector for test classes and functions."""
525+
"""Collector for test classes and functions in a Python module."""
526526

527527
def _getobj(self):
528528
return self._importtestmodule()
@@ -659,6 +659,9 @@ def _importtestmodule(self):
659659

660660

661661
class Package(Module):
662+
"""Collector for files and directories in a Python packages -- directories
663+
with an `__init__.py` file."""
664+
662665
def __init__(
663666
self,
664667
fspath: Optional[LEGACY_PATH],
@@ -788,7 +791,7 @@ def _get_first_non_fixture_func(obj: object, names: Iterable[str]) -> Optional[o
788791

789792

790793
class Class(PyCollector):
791-
"""Collector for test methods."""
794+
"""Collector for test methods (and nested classes) in a Python class."""
792795

793796
@classmethod
794797
def from_parent(cls, parent, *, name, obj=None, **kw):
@@ -1673,7 +1676,7 @@ def write_docstring(tw: TerminalWriter, doc: str, indent: str = " ") -> None:
16731676

16741677

16751678
class Function(PyobjMixin, nodes.Item):
1676-
"""An Item responsible for setting up and executing a Python test function.
1679+
"""Item responsible for setting up and executing a Python test function.
16771680
16781681
:param name:
16791682
The full function name, including any decorations like those
@@ -1830,10 +1833,8 @@ def repr_failure( # type: ignore[override]
18301833

18311834

18321835
class FunctionDefinition(Function):
1833-
"""
1834-
This class is a step gap solution until we evolve to have actual function definition nodes
1835-
and manage to get rid of ``metafunc``.
1836-
"""
1836+
"""This class is a stop gap solution until we evolve to have actual function
1837+
definition nodes and manage to get rid of ``metafunc``."""
18371838

18381839
def runtest(self) -> None:
18391840
raise RuntimeError("function definitions are not supposed to be run as tests")

0 commit comments

Comments
 (0)