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

Raised by Node.from_parent when 'session' appears in the kwargs. Like config, session is inherited from the parent and must not be passed explicitly (lines 223-224). The guard prevents inconsistent session assignment and fails fast before construction proceeds.

Solutions

  1. Drop session= from the from_parent call; it is derived from the parent.
  2. Ensure the parent node belongs to the intended session.
  3. Review from_parent call sites for stray session kwargs.

Example fix

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

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Node.from_parent(parent, session=sess, ...); passing session= as a kwarg to a subclass from_parent; refactoring constructor calls without removing session.

Common situations: Plugin authors copying all Node.__init__ kwargs into from_parent; legacy code that constructed nodes with explicit sessions.

Related errors


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

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