diff --git a/changelog/3616.deprecation.rst b/changelog/3616.deprecation.rst index e91d1ad07..04d4bb509 100644 --- a/changelog/3616.deprecation.rst +++ b/changelog/3616.deprecation.rst @@ -9,3 +9,10 @@ The following accesses have been documented as deprecated for years, but are now * ``request.cached_setup``, this was the precursor of the setup/teardown mechanism available to fixtures. You can consult `funcarg comparision section in the docs `_. + +* Using objects named ``"Class"`` as a way to customize the type of nodes that are collected in ``Collector`` + subclasses has been deprecated. Users instead should use ``pytest_collect_make_item`` to customize node types during + collection. + + This issue should affect only advanced plugins who create new collection types, so if you see this warning + message please contact the authors so they can change the code. diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 222059d8c..8fc11eb4c 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -130,10 +130,13 @@ class Node(object): return getattr(__import__("pytest"), name) else: cls = getattr(self, name) - # TODO: reenable in the features branch - # warnings.warn("use of node.%s is deprecated, " - # "use pytest_pycollect_makeitem(...) to create custom " - # "collection nodes" % name, category=DeprecationWarning) + msg = ( + 'use of special named "%s" objects in collectors of type "%s" to ' + "customize the created nodes is deprecated. " + "Use pytest_pycollect_makeitem(...) to create custom " + "collection nodes instead." % (name, type(self).__name__) + ) + self.warn(RemovedInPytest4Warning(msg)) return cls def __repr__(self): diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 7ca8e6bae..49b21ccb1 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -68,6 +68,36 @@ def test_cached_setup_deprecation(testdir): ) +def test_custom_class_deprecation(testdir): + testdir.makeconftest( + """ + import pytest + + class MyModule(pytest.Module): + + class Class(pytest.Class): + pass + + def pytest_pycollect_makemodule(path, parent): + return MyModule(path, parent) + """ + ) + testdir.makepyfile( + """ + class Test: + def test_foo(self): + pass + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + '*test_custom_class_deprecation.py:1:*"Class" objects in collectors of type "MyModule*', + "*1 passed, 1 warnings in*", + ] + ) + + @pytest.mark.filterwarnings("default") def test_funcarg_prefix_deprecation(testdir): testdir.makepyfile(