Skip to content

Commit d161bed

Browse files
committed
Add an example of how to port the code
1 parent 3637d9e commit d161bed

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

doc/en/deprecations.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ This limitation in api surface intends to enable better/simpler refactoring of t
4545
This means that instead of :code:`MyItem(name="foo", parent=collector, obj=42)`
4646
one now has to invoke :code:`MyItem.from_parent(collector, name="foo")`.
4747

48+
Plugins that wish to support older versions of pytest and suppress the warning can use
49+
`hasattr` to check if `from_parent` exists in that version:
50+
51+
.. code-block:: python
52+
53+
def pytest_pycollect_makeitem(collector, name, obj):
54+
if hasattr(MyItem, "from_parent"):
55+
item = MyItem.from_parent(collector, name="foo")
56+
item.obj = 42
57+
return item
58+
else:
59+
return MyItem(name="foo", parent=collector, obj=42)
60+
4861
Note that ``from_parent`` should only be called with keyword arguments for the parameters.
4962

5063

0 commit comments

Comments
 (0)