pytest-dev/pytest · error · TypeError

session is not a valid argument for from_parent

Error message

session is not a valid argument for from_parent

What it means

Node.from_parent intentionally rejects a `session` keyword. session is inherited from the parent (parent.session) inside from_parent, so an explicit one would be contradictory and is rejected with TypeError.

Source

Thrown at src/_pytest/nodes.py:224

        # 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:
            The warning instance to issue.

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Drop the session kwarg from the from_parent call
  2. If a distinct session is genuinely required, bypass from_parent and use the constructor directly

Example fix

// before
item = MyItem.from_parent(parent, name=n, session=s)
// after
item = MyItem.from_parent(parent, name=n)
Defensive patterns

Strategy: validation

Validate before calling

def safe_from_parent(cls, parent, **kw):
    kw.pop('session', None)  # never forward session
    return cls.from_parent(parent, **kw)

Type guard

def kwargs_have_no_session(kw) -> bool:
    return 'session' not in kw

Prevention

When it happens

Trigger: Calling MyNode.from_parent(parent, session=s, ...) or forwarding session through **kw. Usually leftover from a pre-from_parent constructor pattern.

Common situations: Plugins ported from older APIs that accepted session; overriding from_parent and forwarding **kwargs blindly.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/c04645118cb1695c.json. Report an issue: GitHub.