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

Raised by Node.from_parent when 'config' appears in the kwargs. from_parent deliberately excludes config and session from its accepted kwargs (lines 221-224) because config is always inherited from the parent; passing it would create inconsistency. The guard fails fast with a TypeError before delegating to _create.

Solutions

  1. Remove config= from the from_parent call; config is inherited from parent.
  2. If you need a specific config, ensure the parent node carries it.
  3. Audit from_parent call sites for stray config/session kwargs.

Example fix

// before
item = MyItem.from_parent(parent=collector, name='x', config=config)
// after
item = MyItem.from_parent(parent=collector, name='x')
Defensive patterns

Strategy: type-guard

Validate before calling

kw.pop('config', None)  # never pass config to from_parent

Type guard

def _from_parent_kw_clean(kw) -> bool:
    return 'config' not in kw and 'session' not in kw

Prevention

When it happens

Trigger: Node.from_parent(parent, config=cfg, ...); passing config= as a kwarg to a Node subclass's from_parent; copying constructor kwargs into from_parent calls.

Common situations: Migrating from direct Node(...) construction to from_parent without stripping config; plugin code that threads config through kwargs.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/91cab732c8fd9b15. Report an issue: GitHub.

Appendix: 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 0d6fbdeffa)