From 3d390940d1ad895527486ea69fd06d68732b677b Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 6 Feb 2020 00:00:02 +0100 Subject: [PATCH 1/4] refer the node-from-parent deprecation documentation in the warning fixup: fix test for warning --- src/_pytest/deprecated.py | 5 ++++- testing/deprecated_test.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index db43c6ca3ef..cd9ed97d89c 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -36,7 +36,10 @@ NODE_USE_FROM_PARENT = UnformattedWarning( PytestDeprecationWarning, - "direct construction of {name} has been deprecated, please use {name}.from_parent", + "Direct construction of {name} has been deprecated, please use {name}.from_parent.\n" + "See " + "https://docs.pytest.org/en/latest/deprecations.html#node-construction-changed-to-node-from-parent" + " for more details.", ) JUNIT_XML_DEFAULT_FAMILY = PytestDeprecationWarning( diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index b5c66d9f5f1..98c535aed6b 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -86,7 +86,7 @@ class MockConfig: ms = MockConfig() with pytest.warns( DeprecationWarning, - match="direct construction of .* has been deprecated, please use .*.from_parent", + match="Direct construction of .* has been deprecated, please use .*.from_parent.*", ) as w: nodes.Node(name="test", config=ms, session=ms, nodeid="None") assert w[0].lineno == inspect.currentframe().f_lineno - 1 From 5c1e56d3505e42e258a4aed4a38836ac82def810 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 18 Feb 2020 13:45:29 +0100 Subject: [PATCH 2/4] docs: from_parent - add minimal before/after example fixup: fix from_parent version --- doc/en/deprecations.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index 732f92985f1..6956d0aaacf 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -29,7 +29,7 @@ Below is a complete list of all pytest features which are considered deprecated. Option ``--no-print-logs`` is deprecated and meant to be removed in a future release. If you use ``--no-print-logs``, please try out ``--show-capture`` and provide feedback. -``--show-capture`` command-line option was added in ``pytest 3.5.0` and allows to specify how to +``--show-capture`` command-line option was added in ``pytest 3.5.0`` and allows to specify how to display captured output when tests fail: ``no``, ``stdout``, ``stderr``, ``log`` or ``all`` (the default). @@ -39,9 +39,13 @@ Node Construction changed to ``Node.from_parent`` .. deprecated:: 5.4 -The construction of nodes new should use the named constructor ``from_parent``. +The construction of nodes now should use the named constructor ``from_parent``. This limitation in api surface intends to enable better/simpler refactoring of the collection tree. +This means that instead of :code:`MyItem(name="foo", parent=collector, obj=42)` +one now has to invoke :code:`MyItem.from_parent(collector, name="foo")`. + + ``junit_family`` default value change to "xunit2" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 3637d9eb3fde10f12abfe2966bcdd30fc9a8808e Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 22 Feb 2020 17:09:16 +0100 Subject: [PATCH 3/4] followup: add note on from_parent kwargs --- doc/en/deprecations.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index 6956d0aaacf..b707caa1310 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -45,6 +45,8 @@ This limitation in api surface intends to enable better/simpler refactoring of t This means that instead of :code:`MyItem(name="foo", parent=collector, obj=42)` one now has to invoke :code:`MyItem.from_parent(collector, name="foo")`. +Note that ``from_parent`` should only be called with keyword arguments for the parameters. + ``junit_family`` default value change to "xunit2" From d161bedcee9b09dba53c66d12a60a3df8adb8e17 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 4 Mar 2020 09:23:31 -0300 Subject: [PATCH 4/4] Add an example of how to port the code --- doc/en/deprecations.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index b707caa1310..13d59bce2b7 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -45,6 +45,19 @@ This limitation in api surface intends to enable better/simpler refactoring of t This means that instead of :code:`MyItem(name="foo", parent=collector, obj=42)` one now has to invoke :code:`MyItem.from_parent(collector, name="foo")`. +Plugins that wish to support older versions of pytest and suppress the warning can use +`hasattr` to check if `from_parent` exists in that version: + +.. code-block:: python + + def pytest_pycollect_makeitem(collector, name, obj): + if hasattr(MyItem, "from_parent"): + item = MyItem.from_parent(collector, name="foo") + item.obj = 42 + return item + else: + return MyItem(name="foo", parent=collector, obj=42) + Note that ``from_parent`` should only be called with keyword arguments for the parameters.