Skip to content

How to address DeprecationWarning with Node.from_parent #6928

@jaraco

Description

@jaraco

In jaraco/pytest-checkdocs#3, I started observing a warning in my projects.

.tox/python/lib/python3.8/site-packages/pytest_checkdocs.py:12
  /Users/jaraco/code/main/jaraco.util/.tox/python/lib/python3.8/site-packages/pytest_checkdocs.py:12: PytestDeprecationWarning: direct construction of CheckdocsItem has been deprecated, please use CheckdocsItem.from_parent
    return CheckdocsItem(path, parent) if path.basename == 'setup.py' else None

I see similar warnings in pytest-flake8 and pytest-black.

.tox/python/lib/python3.8/site-packages/pytest_flake8.py:65
  /Users/jaraco/code/main/pytest-checkdocs/.tox/python/lib/python3.8/site-packages/pytest_flake8.py:65: PytestDeprecationWarning: direct construction of Flake8Item has been deprecated, please use Flake8Item.from_parent
    return Flake8Item(

.tox/python/lib/python3.8/site-packages/pytest_black.py:26
  /Users/jaraco/code/main/pytest-checkdocs/.tox/python/lib/python3.8/site-packages/pytest_black.py:26: PytestDeprecationWarning: direct construction of BlackItem has been deprecated, please use BlackItem.from_parent
    return BlackItem(path, parent)

I've attempted to port this code to the new recommended format, but I haven't been able to figure out the syntax.

I tried this patch:

diff --git a/pytest_checkdocs.py b/pytest_checkdocs.py
index 970cf26..9db4a0a 100644
--- a/pytest_checkdocs.py
+++ b/pytest_checkdocs.py
@@ -9,7 +9,11 @@ from more_itertools import first
 
 def pytest_collect_file(path, parent):
     """Filter files down to which ones should be checked."""
-    return CheckdocsItem(path, parent) if path.basename == 'setup.py' else None
+    return (
+        CheckdocsItem.from_parent(parent, name=path)
+        if path.basename == 'setup.py'
+        else None
+    )
 
 
 class CheckdocsItem(pytest.Item, pytest.File):

But that gives me an error:

_____________________________________________________________________ ERROR collecting test session ______________________________________________________________________
.tox/python/lib/python3.8/site-packages/pluggy/hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
.tox/python/lib/python3.8/site-packages/pluggy/manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
.tox/python/lib/python3.8/site-packages/pluggy/manager.py:84: in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
pytest_checkdocs.py:13: in pytest_collect_file
    CheckdocsItem.from_parent(parent, name=path)
E   TypeError: from_parent() got an unexpected keyword argument 'name'

I don't understand how from_parent could have an unexpected keyword argument when it accepts all keyword arguments.

What is the proper way to implement a hook in pytest_collect_file to construct a Node/Item?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions