pytest-dev/pytest · error · TypeError
session or parent must be provided
Error message
session or parent must be provided
What it means
Raised by Node.__init__ when neither session nor parent is supplied. Mirroring the config check, lines 172-177 fall back to parent.session when session is None; absent both, the node has no session to anchor its hook proxy and nodeid, so construction is rejected. The session is load-bearing for ihook dispatch and test lifecycle.
Solutions
- Use Node.from_parent(parent, **kw) so session is inherited from parent.
- Pass an explicit session object when constructing standalone.
- Ensure a parent node is always provided in the construction chain.
Example fix
// before node = MyItem(name='x') // after node = MyItem.from_parent(parent=collector, name='x')
Defensive patterns
Strategy: type-guard
Validate before calling
assert session is not None or parent is not None, 'session or parent required'
Type guard
def _node_has_session_source(session, parent) -> bool:
return session is not None or parent is not None Prevention
- Construct nodes via from_parent to inherit session.
- Pass the session fixture through when building standalone nodes.
When it happens
Trigger: Node(...) with session=None and parent=None; constructing a node detached from the Session, e.g. in a unit test or a buggy plugin that bypasses from_parent.
Common situations: Plugin authors building nodes manually without a parent; mocking frameworks that construct nodes without a session fixture.
Related errors
- config is not a valid argument for from_parent
- config or parent must be provided
- nodeid or parent must be provided
- session is not a valid argument for from_parent
- expected string as 'msg' parameter, got ' '…
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/d2ccbefe110661f1.
Report an issue: GitHub.
Appendix: source
Thrown at src/_pytest/nodes.py:176
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.
self.keywords: MutableMapping[str, Any] = NodeKeywords(self)
#: The marker objects belonging to this node.
self.own_markers: list[Mark] = []
#: Allow adding of extra keywords to use for matching.
self.extra_keyword_matches: set[str] = set()
View on GitHub (pinned to 0d6fbdeffa)