Skip to content

Commit 663be09

Browse files
The-Compilerpre-commit-ci[bot]
andauthoredDec 6, 2021
Improve reference and path/fspath docs (#9341)
* Improve reference and path/fspath docs Closes #9283 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixups * Add explanation * Update wording after #9363 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5852d66 commit 663be09

File tree

7 files changed

+95
-16
lines changed

7 files changed

+95
-16
lines changed
 

‎changelog/7259.deprecation.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
``py.path.local`` arguments for hooks have been deprecated. See :ref:`the deprecation note <legacy-path-hooks-deprecated>` for full details.
22

33
``py.path.local`` arguments to Node constructors have been deprecated. See :ref:`the deprecation note <node-ctor-fspath-deprecation>` for full details.
4+
5+
.. note::
6+
The name of the :class:`~_pytest.nodes.Node` arguments and attributes (the
7+
new attribute being ``path``) is **the opposite** of the situation for hooks
8+
(the old argument being ``path``).
9+
10+
This is an unfortunate artifact due to historical reasons, which should be
11+
resolved in future versions as we slowly get rid of the :pypi:`py`
12+
dependency (see :issue:`9283` for a longer discussion).

‎changelog/8144.feature.rst

+9
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ 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 :class:`~_pytest.nodes.Node` arguments and attributes (the
11+
new attribute being ``path``) is **the opposite** of the situation for hooks
12+
(the old argument being ``path``).
13+
14+
This is an unfortunate artifact due to historical reasons, which should be
15+
resolved in future versions as we slowly get rid of the :pypi:`py`
16+
dependency (see :issue:`9283` for a longer discussion).

‎changelog/8251.feature.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
Implement ``Node.path`` as a ``pathlib.Path``.
1+
Implement ``Node.path`` as a ``pathlib.Path``. Both the old ``fspath`` and this new attribute gets set no matter whether ``path`` or ``fspath`` (deprecated) 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 :class:`~_pytest.nodes.Node` arguments and attributes (the
6+
new attribute being ``path``) is **the opposite** of the situation for hooks
7+
(the old argument being ``path``).
8+
9+
This is an unfortunate artifact due to historical reasons, which should be
10+
resolved in future versions as we slowly get rid of the :pypi:`py`
11+
dependency (see :issue:`9283` for a longer discussion).

‎changelog/9341.doc.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Various methods commonly used for :ref:`non-python tests` are now correctly documented in the reference docs. They were undocumented previously.

‎doc/en/deprecations.rst

+27-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,24 @@ 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 :class:`~_pytest.nodes.Node` arguments and attributes (the
61+
new attribute being ``path``) is **the opposite** of the situation for
62+
hooks, :ref:`outlined below <legacy-path-hooks-deprecated>` (the old
63+
argument being ``path``).
64+
65+
This is an unfortunate artifact due to historical reasons, which should be
66+
resolved in future versions as we slowly get rid of the :pypi:`py`
67+
dependency (see :issue:`9283` for a longer discussion).
68+
69+
Due to the ongoing migration of methods like :meth:`~_pytest.Item.reportinfo`
70+
which still is expected to return a ``py.path.local`` object, nodes still have
71+
both ``fspath`` (``py.path.local``) and ``path`` (``pathlib.Path``) attributes,
72+
no matter what argument was used in the constructor. We expect to deprecate the
73+
``fspath`` attribute in a future release.
5974

6075
.. _legacy-path-hooks-deprecated:
6176

@@ -74,6 +89,16 @@ In order to support the transition from ``py.path.local`` to :mod:`pathlib`, the
7489

7590
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.
7691

92+
.. note::
93+
The name of the :class:`~_pytest.nodes.Node` arguments and attributes,
94+
:ref:`outlined above <node-ctor-fspath-deprecation>` (the new attribute
95+
being ``path``) is **the opposite** of the situation for hooks (the old
96+
argument being ``path``).
97+
98+
This is an unfortunate artifact due to historical reasons, which should be
99+
resolved in future versions as we slowly get rid of the :pypi:`py`
100+
dependency (see :issue:`9283` for a longer discussion).
101+
77102
Directly constructing internal classes
78103
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79104

‎src/_pytest/nodes.py

+29-4
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

+9-9
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)
Please sign in to comment.