From d161bedcee9b09dba53c66d12a60a3df8adb8e17 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 4 Mar 2020 09:23:31 -0300 Subject: [PATCH] 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 b707caa13..13d59bce2 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.