pytest-dev/pytest · error · AttributeError
module not available in
Error message
module not available in {self.scope}-scoped context What it means
request.module returns the Python module where the test was collected, but only for 'function', 'class', or 'module' scopes. In a session or package scope there is no single module, so pytest raises AttributeError naming the offending scope.
Solutions
- Access request.module from within a module/class/function-scoped fixture instead.
- If a wider fixture needs module-specific data, depend on a module-scoped fixture that yields it.
- Guard access: 'if request.scope in ("function", "class", "module"):'.
Example fix
// before
@pytest.fixture(scope='session')
def sess(request):
return request.module.__name__
// after
@pytest.fixture(scope='module')
def mod(request):
return request.module.__name__ Defensive patterns
Strategy: type-guard
Validate before calling
null
Type guard
def has_module_scope(request) -> bool:
return getattr(request, 'scope', None) in ('function', 'class', 'module')
if has_module_scope(request):
mod = request.module Try / catch
null
Prevention
- Only access request.module at function/class/module scope.
- Use a module-scoped fixture to pass module data upward.
When it happens
Trigger: Calling request.module from a session-scoped fixture; a package-scoped fixture reaching for the module object.
Common situations: Widening a fixture's scope during refactoring; writing a session-level autouse fixture that assumed module context.
Related errors
- cls not available in
- function not available in
- path 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/2555766fb0e0a074.
Report an issue: GitHub.
Appendix: source
Thrown at src/_pytest/fixtures.py:607
"""Class (can be None) where the test function was collected."""
if self.scope not in ("class", "function"):
raise AttributeError(f"cls not available in {self.scope}-scoped context")
clscol = self._pyfuncitem.getparent(_pytest.python.Class)
if clscol:
return clscol.obj
@property
def instance(self):
"""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
@propertyView on GitHub (pinned to 0d6fbdeffa)