Skip to content

Commit 70b989d

Browse files
committed
Improve reference and path/fspath docs
Closes pytest-dev#9283
1 parent a335ade commit 70b989d

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

changelog/8144.feature.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ The following hooks now receive an additional ``pathlib.Path`` argument, equival
55
- :func:`pytest_pycollect_makemodule <_pytest.hookspec.pytest_pycollect_makemodule>` - The ``module_path`` parameter (equivalent to existing ``path`` parameter).
66
- :func:`pytest_report_header <_pytest.hookspec.pytest_report_header>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
77
- :func:`pytest_report_collectionfinish <_pytest.hookspec.pytest_report_collectionfinish>` - The ``start_path`` parameter (equivalent to existing ``startdir`` parameter).
8+
9+
.. note::
10+
The name of the ``Node`` arguments and attributes (the new attribute being
11+
``path``) is **the opposite** of the situation for hooks (the old argument
12+
being ``path``).

changelog/8251.feature.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
Implement ``Node.path`` as a ``pathlib.Path``.
1+
Implement ``Node.path`` as a ``pathlib.Path``. This attribute gets set no matter whether ``path`` or (deprecated) ``fspath`` is passed to the constructor. It is a replacement for the ``fspath`` attribute (which represents the same path as ``py.path.local``). While ``fspath`` is not deprecated yet
2+
due to the ongoing migration of methods like :meth:`~_pytest.Item.reportinfo`, we expect to deprecate it in a future release.
3+
4+
.. note::
5+
The name of the attributes (old ``fspath``, new ``path``) is **the opposite**
6+
of the situation for hooks.

doc/en/deprecations.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,18 @@ Plugins which construct nodes should pass the ``path`` argument, of type
5353
:class:`pathlib.Path`, instead of the ``fspath`` argument.
5454

5555
Plugins which implement custom items and collectors are encouraged to replace
56-
``py.path.local`` ``fspath`` parameters with ``pathlib.Path`` parameters, and
57-
drop any other usage of the ``py`` library if possible.
56+
``fspath`` parameters (``py.path.local``) with ``path`` parameters
57+
(``pathlib.Path``), and drop any other usage of the ``py`` library if possible.
5858

59+
.. note::
60+
The name of the arguments (old ``fspath``, new ``path``) is **the opposite**
61+
of the situation for hooks, :ref:`outlined below <_legacy-path-hooks-deprecated>`.
62+
63+
Due to the ongoing migration of methods like :meth:`~_pytest.Item.reportinfo`
64+
which still is expected to return a ``py.path.local`` object, nodes still have
65+
both ``fspath`` (``py.path.local``) and ``path`` (``pathlib.Path``) attributes,
66+
no matter what argument was used in the constructor. We expect to deprecate the
67+
``fspath`` attribute in a future release.
5968

6069
.. _legacy-path-hooks-deprecated:
6170

@@ -74,6 +83,10 @@ In order to support the transition from ``py.path.local`` to :mod:`pathlib`, the
7483

7584
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.
7685

86+
.. note::
87+
The name of the arguments (old ``path``, new ``fspath``) is **the opposite**
88+
of the situation for the :class:`~_pytest.nodes.Node` class, :ref:`outlined above <_node-ctor-fspath-deprecation>`.
89+
7790
Directly constructing internal classes
7891
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7992

src/_pytest/nodes.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ class Node(metaclass=NodeMeta):
159159
Collector subclasses have children; Items are leaf nodes.
160160
"""
161161

162+
# Implemented in the legacypath plugin.
163+
#: A ``LEGACY_PATH`` copy of the :attr:`path` attribute. Intended for usage
164+
#: for methods not migrated to ``pathlib.Path`` yet, such as
165+
#: :meth:`Item.reportinfo`. Will be deprecated in a future release, prefer
166+
#: using :attr:`path` instead.
167+
fspath: LEGACY_PATH
168+
162169
# Use __slots__ to make attribute access faster.
163170
# Note that __dict__ is still available.
164171
__slots__ = (
@@ -188,26 +195,26 @@ def __init__(
188195
#: The parent collector node.
189196
self.parent = parent
190197

191-
#: The pytest config object.
192198
if config:
199+
#: The pytest config object.
193200
self.config: Config = config
194201
else:
195202
if not parent:
196203
raise TypeError("config or parent must be provided")
197204
self.config = parent.config
198205

199-
#: The pytest session this node is part of.
200206
if session:
207+
#: The pytest session this node is part of.
201208
self.session = session
202209
else:
203210
if not parent:
204211
raise TypeError("session or parent must be provided")
205212
self.session = parent.session
206213

207-
#: Filesystem path where this node was collected from (can be None).
208214
if path is None and fspath is None:
209215
path = getattr(parent, "path", None)
210-
self.path = _imply_path(type(self), path, fspath=fspath)
216+
#: Filesystem path where this node was collected from (can be None).
217+
self.path: Path = _imply_path(type(self), path, fspath=fspath)
211218

212219
# The explicit annotation is to avoid publicly exposing NodeKeywords.
213220
#: Keywords/markers collected from all scopes.
@@ -478,6 +485,8 @@ def repr_failure(
478485
) -> Union[str, TerminalRepr]:
479486
"""Return a representation of a collection or test failure.
480487
488+
.. seealso:: :ref:`non-python tests`
489+
481490
:param excinfo: Exception information for the failure.
482491
"""
483492
return self._repr_failure_py(excinfo, style)
@@ -686,6 +695,12 @@ def __init__(
686695
self.user_properties: List[Tuple[str, object]] = []
687696

688697
def runtest(self) -> None:
698+
"""Run the test case for this item.
699+
700+
Must be implemented by subclasses.
701+
702+
.. seealso:: :ref:`non-python tests`
703+
"""
689704
raise NotImplementedError("runtest must be implemented by Item subclass")
690705

691706
def add_report_section(self, when: str, key: str, content: str) -> None:
@@ -706,6 +721,16 @@ def add_report_section(self, when: str, key: str, content: str) -> None:
706721
self._report_sections.append((when, key, content))
707722

708723
def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]:
724+
"""Get location information for this item for test reports.
725+
726+
Returns a tuple with three elements:
727+
728+
- The path of the test (default ``self.path``)
729+
- The line number of the test (default ``None``)
730+
- A name of the test to be shown (default ``""``)
731+
732+
.. seealso:: :ref:`non-python tests`
733+
"""
709734
return self.path, None, ""
710735

711736
@cached_property

src/_pytest/python.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,26 +1578,26 @@ def write_docstring(tw: TerminalWriter, doc: str, indent: str = " ") -> None:
15781578
class Function(PyobjMixin, nodes.Item):
15791579
"""An Item responsible for setting up and executing a Python test function.
15801580
1581-
param name:
1581+
:param name:
15821582
The full function name, including any decorations like those
15831583
added by parametrization (``my_func[my_param]``).
1584-
param parent:
1584+
:param parent:
15851585
The parent Node.
1586-
param config:
1586+
:param config:
15871587
The pytest Config object.
1588-
param callspec:
1588+
:param callspec:
15891589
If given, this is function has been parametrized and the callspec contains
15901590
meta information about the parametrization.
1591-
param callobj:
1591+
:param callobj:
15921592
If given, the object which will be called when the Function is invoked,
15931593
otherwise the callobj will be obtained from ``parent`` using ``originalname``.
1594-
param keywords:
1594+
:param keywords:
15951595
Keywords bound to the function object for "-k" matching.
1596-
param session:
1596+
:param session:
15971597
The pytest Session object.
1598-
param fixtureinfo:
1598+
:param fixtureinfo:
15991599
Fixture information already resolved at this fixture node..
1600-
param originalname:
1600+
:param originalname:
16011601
The attribute name to use for accessing the underlying function object.
16021602
Defaults to ``name``. Set this if name is different from the original name,
16031603
for example when it contains decorations like those added by parametrization

0 commit comments

Comments
 (0)