pytest-dev/pytest · error · AttributeError
cls not available in
Error message
cls not available in {self.scope}-scoped context What it means
request.cls returns the class in which the test function was collected, but only for 'class' or 'function' scopes. In a module/session/package scope there is no single class context, so pytest raises AttributeError naming the scope.
Solutions
- Move the request.cls access into a class- or function-scoped fixture.
- If you need class data in a wider scope, pass it up via an explicit fixture dependency rather than reaching through request.
- Guard with 'if request.scope in ("class", "function"):'.
Example fix
// before
@pytest.fixture(scope='module')
def mod(request):
return request.cls
// after
@pytest.fixture
def per_class(request):
return request.cls Defensive patterns
Strategy: type-guard
Validate before calling
null
Type guard
def has_cls_scope(request) -> bool:
return getattr(request, 'scope', None) in ('class', 'function')
if has_cls_scope(request):
cls = request.cls Try / catch
null
Prevention
- Restrict request.cls access to class/function scope.
- Prefer explicit fixture dependencies over reaching through request.
When it happens
Trigger: Calling request.cls inside a session-scoped fixture; a module-scoped fixture that tries to read the test class.
Common situations: Promoting a function-scoped fixture to module scope; running a fixture from a non-class test where cls would be None anyway.
Related errors
- function not available in
- module 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/3a244824306af4a2.
Report an issue: GitHub.
Appendix: source
Thrown at src/_pytest/fixtures.py:591
@property
def config(self) -> Config:
"""The pytest config object associated with this request."""
return self._pyfuncitem.config
@property
def function(self):
"""Test function object if the request has a per-function scope."""
if self.scope != "function":
raise AttributeError(
f"function not available in {self.scope}-scoped context"
)
return self._pyfuncitem.obj
@property
def cls(self):
"""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 NoneView on GitHub (pinned to 0d6fbdeffa)