Skip to content

Commit 28e5b3b

Browse files
[7.0.x] Add additional docs for uncooperative ctor deprecation (#9552)
Co-authored-by: Florian Bruhin <[email protected]>
1 parent e854d05 commit 28e5b3b

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

doc/en/deprecations.rst

+38
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Plugins which implement custom items and collectors are encouraged to replace
5656
``fspath`` parameters (``py.path.local``) with ``path`` parameters
5757
(``pathlib.Path``), and drop any other usage of the ``py`` library if possible.
5858

59+
If possible, plugins with custom items should use :ref:`cooperative
60+
constructors <uncooperative-constructors-deprecated>` to avoid hardcoding
61+
arguments they only pass on to the superclass.
62+
5963
.. note::
6064
The name of the :class:`~_pytest.nodes.Node` arguments and attributes (the
6165
new attribute being ``path``) is **the opposite** of the situation for
@@ -191,6 +195,40 @@ Instead, a separate collector node should be used, which collects the item. See
191195
.. _example pr fixing inheritance: https://github.com/asmeurer/pytest-flakes/pull/40/files
192196

193197

198+
.. _uncooperative-constructors-deprecated:
199+
200+
Constructors of custom :class:`pytest.Node` subclasses should take ``**kwargs``
201+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202+
203+
.. deprecated:: 7.0
204+
205+
If custom subclasses of nodes like :class:`pytest.Item` override the
206+
``__init__`` method, they should take ``**kwargs``. Thus,
207+
208+
.. code-block:: python
209+
210+
class CustomItem(pytest.Item):
211+
def __init__(self, name, parent, additional_arg):
212+
super().__init__(name, parent)
213+
self.additional_arg = additional_arg
214+
215+
should be turned into:
216+
217+
.. code-block:: python
218+
219+
class CustomItem(pytest.Item):
220+
def __init__(self, *, additional_arg, **kwargs):
221+
super().__init__(**kwargs)
222+
self.additional_arg = additional_arg
223+
224+
to avoid hard-coding the arguments pytest can pass to the superclass.
225+
See :ref:`non-python tests` for a full example.
226+
227+
For cases without conflicts, no deprecation warning is emitted. For cases with
228+
conflicts (such as :class:`pytest.File` now taking ``path`` instead of
229+
``fspath``, as :ref:`outlined above <node-ctor-fspath-deprecation>`), a
230+
deprecation warning is now raised.
231+
194232
Backward compatibilities in ``Parser.addoption``
195233
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
196234

doc/en/example/nonpython/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def collect(self):
1818

1919

2020
class YamlItem(pytest.Item):
21-
def __init__(self, name, parent, spec):
22-
super().__init__(name, parent)
21+
def __init__(self, *, spec, **kwargs):
22+
super().__init__(**kwargs)
2323
self.spec = spec
2424

2525
def runtest(self):

src/_pytest/nodes.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ def _create(self, *k, **kw):
145145

146146
warnings.warn(
147147
PytestDeprecationWarning(
148-
f"{self} is not using a cooperative constructor and only takes {set(known_kw)}"
148+
f"{self} is not using a cooperative constructor and only takes {set(known_kw)}.\n"
149+
"See https://docs.pytest.org/en/stable/deprecations.html"
150+
"#constructors-of-custom-pytest-node-subclasses-should-take-kwargs "
151+
"for more details."
149152
)
150153
)
151154

0 commit comments

Comments
 (0)