Add an example of how to port the code

This commit is contained in:
Bruno Oliveira 2020-03-04 09:23:31 -03:00
parent 3637d9eb3f
commit d161bedcee
1 changed files with 13 additions and 0 deletions

View File

@ -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)` 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")`. 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. Note that ``from_parent`` should only be called with keyword arguments for the parameters.