pytest-dev/pytest · error · TypeError

config or parent must be provided

Error message

config or parent must be provided

What it means

Raised by Node.__init__ when neither config nor parent is supplied. The constructor at lines 165-170 falls back to parent.config when config is None, so a missing parent with no config leaves no way to obtain the Config object, which is required for every node. This guards against constructing an orphan node that cannot participate in pytest's config/session graph.

Solutions

  1. Construct nodes via Node.from_parent(parent, **kw) which propagates config from the parent.
  2. If constructing standalone, pass an explicit config object.
  3. Pass a real parent node so config is inherited.

Example fix

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

Strategy: type-guard

Validate before calling

assert config is not None or parent is not None, 'config or parent required'

Type guard

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

Prevention

When it happens

Trigger: Calling Node(...) or a subclass constructor directly with config=None and parent=None; instantiating a custom Node subclass without routing through from_parent; building a node in a plugin without a parent collector.

Common situations: Plugin authors instantiate Item/Collector subclasses manually instead of using from_parent; tests that construct nodes standalone for unit testing without stubbing parent.

Related errors


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

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