pytest-dev/pytest · error · AttributeError
path not available in
Error message
path not available in {self.scope}-scoped context What it means
request.path returns the filesystem path where the test was collected, available in 'function', 'class', 'module', and 'package' scopes. In a session scope there is no single path, so pytest raises AttributeError naming 'session' as the scope.
Solutions
- Move the request.path access into a function/class/module/package-scoped fixture.
- If you need the path in a wider scope, depend on a narrower fixture that passes the path up.
- Guard access with 'if request.scope in ("function", "class", "module", "package"):'.
Example fix
// before
@pytest.fixture(scope='session')
def sess(request):
return request.path
// after
@pytest.fixture
def per_test(request):
return request.path Defensive patterns
Strategy: type-guard
Validate before calling
null
Type guard
def has_path_scope(request) -> bool:
return getattr(request, 'scope', None) in ('function', 'class', 'module', 'package')
if has_path_scope(request):
p = request.path Try / catch
null
Prevention
- Do not call request.path from session-scoped fixtures.
- Access path in a narrower fixture and pass it up if needed.
When it happens
Trigger: Calling request.path from a session-scoped fixture; an autouse session fixture reaching for the test file path.
Common situations: Promoting a fixture to session scope; writing session-level setup that assumed per-file context.
Related errors
- cls not available in
- function not available in
- module not available in
- Error evaluating while defining fixture ' '. Expected a…
- is the lower-most scope
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/98e1b970a68f6922.
Report an issue: GitHub.
Appendix: source
Thrown at src/_pytest/fixtures.py:616
"""Instance (can be None) on which test function was collected."""
if self.scope != "function":
return None
return getattr(self._pyfuncitem, "instance", None)
@property
def module(self):
"""Python module object where the test function was collected."""
if self.scope not in ("function", "class", "module"):
raise AttributeError(f"module not available in {self.scope}-scoped context")
mod = self._pyfuncitem.getparent(_pytest.python.Module)
assert mod is not None
return mod.obj
@property
def path(self) -> Path:
"""Path where the test function was collected."""
if self.scope not in ("function", "class", "module", "package"):
raise AttributeError(f"path not available in {self.scope}-scoped context")
return self._pyfuncitem.path
@property
def keywords(self) -> MutableMapping[str, Any]:
"""Keywords/markers dictionary for the underlying node."""
node: nodes.Node = self.node
return node.keywords
@property
def session(self) -> Session:
"""Pytest session object."""
return self._pyfuncitem.session
@abc.abstractmethod
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
"""Add finalizer/teardown function to be called without arguments after
the last test within the requesting test context finished execution."""
raise NotImplementedError()View on GitHub (pinned to 0d6fbdeffa)