pytest-dev/pytest · error · TypeError
config is not a valid argument for from_parent
Error message
config is not a valid argument for from_parent
What it means
Node.from_parent intentionally rejects a `config` keyword. config and session are always inherited from the parent in from_parent, so passing them would create inconsistency. The guard forces correct usage of the public constructor.
Source
Thrown at src/_pytest/nodes.py:222
#: own use.
self.stash: Stash = Stash()
# Deprecated alias. Was never public. Can be removed in a few releases.
self._store = self.stash
@classmethod
def from_parent(cls, parent: Node, **kw) -> Self:
"""Public constructor for Nodes.
This indirection got introduced in order to enable removing
the fragile logic from the node constructors.
Subclasses can use ``super().from_parent(...)`` when overriding the
construction.
:param parent: The parent node of this Node.
"""
if "config" in kw:
raise TypeError("config is not a valid argument for from_parent")
if "session" in kw:
raise TypeError("session is not a valid argument for from_parent")
return cls._create(parent=parent, **kw)
@property
def ihook(self) -> pluggy.HookRelay:
"""Path-sensitive hook proxy used to call pytest hooks."""
return self.session.gethookproxy(self.path)
def __repr__(self) -> str:
return "<{} {}>".format(self.__class__.__name__, getattr(self, "name", None))
def warn(self, warning: Warning) -> None:
"""Issue a warning for this Node.
Warnings will be displayed after the test session, unless explicitly suppressed.
:param Warning warning:View on GitHub (pinned to 98b357f69e)
Solutions
- Remove the config kwarg from the from_parent call; config is derived from the parent
- If you truly need a different config, call the constructor (_create / __init__) directly instead of from_parent
Example fix
// before item = MyItem.from_parent(parent, name=n, config=cfg) // after item = MyItem.from_parent(parent, name=n)
Defensive patterns
Strategy: validation
Validate before calling
def safe_from_parent(cls, parent, **kw):
kw.pop('config', None) # never forward config
return cls.from_parent(parent, **kw) Type guard
def kwargs_have_no_config(kw) -> bool:
return 'config' not in kw Prevention
- Never forward config through from_parent; it is inherited
- Audit from_parent overrides for stray **kwargs leaking config
When it happens
Trigger: Calling MyNode.from_parent(parent, config=cfg, ...) or passing config via **kw to from_parent. Plugin code migrated from the old constructor pattern often leaks config through.
Common situations: Older pytest plugins/tutorials that passed config= to constructors; copy-paste from __init__ into a from_parent override.
Related errors
- session is not a valid argument for from_parent
- config or parent must be provided
- session or parent must be provided
- nodeid or parent must be provided
- runtest must be implemented by Item subclass
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/91cab732c8fd9b15.json.
Report an issue: GitHub.