pytest-dev/pytest · error · TypeError

config or parent must be provided

Error message

config or parent must be provided

What it means

Node.__init__ requires either a parent node (config is then inherited from parent.config) or an explicit config object. Constructing a Node with neither makes the node unusable, so pytest raises TypeError. Hits plugin authors subclassing Node/Collector/Item directly.

Source

Thrown at src/_pytest/nodes.py:168

        parent: Node | None = None,
        config: Config | None = None,
        session: Session | None = None,
        fspath: None = None,
        path: Path | None = None,
        nodeid: str | None = None,
    ) -> None:
        #: A unique name within the scope of the parent node.
        self.name: str = name

        #: The parent collector node.
        self.parent = parent

        if config:
            #: The pytest config object.
            self.config: Config = config
        else:
            if not parent:
                raise TypeError("config or parent must be provided")
            self.config = parent.config

        if session:
            #: The pytest session this node is part of.
            self.session: Session = session
        else:
            if not parent:
                raise TypeError("session or parent must be provided")
            self.session = parent.session

        if path is None:
            assert parent is not None
            path = parent.path
        #: Filesystem path where this node was collected from.
        self.path: pathlib.Path = path

        # The explicit annotation is to avoid publicly exposing NodeKeywords.
        #: Keywords/markers collected from all scopes.

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Pass a parent node: MyNode(name, parent=parent_node)
  2. If root-level, pass both config and session explicitly along with nodeid

Example fix

// before
node = MyNode('test_x')
// after
node = MyNode('test_x', parent=parent_node)
Defensive patterns

Strategy: validation

Validate before calling

def make_node(cls, name, *, parent=None, config=None, **kw):
    if parent is None and config is None:
        raise TypeError("provide parent or config")
    return cls(name, parent=parent, config=config, **kw)

Type guard

def has_parent_or_config(parent, config) -> bool:
    return parent is not None or config is not None

Prevention

When it happens

Trigger: Instantiating a Node subclass like MyNode('name') with no parent and no config keyword. Most common when a plugin bypasses Node.from_parent and calls the constructor manually.

Common situations: Custom collector/item plugins that construct nodes without forwarding the parent; mis-copied constructor calls; refactor that dropped the parent argument.

Related errors


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