pytest-dev/pytest · error · AttributeError
function not available in
Error message
function not available in {self.scope}-scoped context What it means
request.function returns the test function object, but only when the current request scope is 'function'. Accessing it from a wider scope (module, package, session) raises AttributeError because there is no single per-function target. The message names the offending scope.
Solutions
- Move the request.function access into a function-scoped fixture or test body.
- If you need the function in a wider scope, pass it in explicitly via a parametrized fixture or a function-scoped fixture that depends on the wider one.
- Guard with 'if request.scope == "function":' before accessing .function.
Example fix
// before
@pytest.fixture(scope='session')
def sess(request):
return request.function.__name__
// after
@pytest.fixture(scope='session')
def sess():
return 'session-wide'
@pytest.fixture
def per_test(request, sess):
return request.function.__name__ Defensive patterns
Strategy: type-guard
Validate before calling
null
Type guard
def is_function_scope(request) -> bool:
return getattr(request, 'scope', None) == 'function'
# usage
if is_function_scope(request):
fn = request.function Try / catch
null
Prevention
- Keep request.function access inside function-scoped fixtures.
- When widening a fixture's scope, audit every request.* access.
When it happens
Trigger: Calling request.function inside a session- or module-scoped fixture; using request.getfixturevalue('request').function in setup of a class-scoped fixture.
Common situations: Refactoring a function-scoped fixture to module scope without removing request.function usage; copying fixture code into a conftest session fixture.
Related errors
- cls 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/e2cb6adfc430c2c5.
Report an issue: GitHub.
Appendix: source
Thrown at src/_pytest/fixtures.py:582
result.extend(set(self._fixture_defs).difference(result))
return result
@property
@abc.abstractmethod
def node(self):
"""Underlying collection node (depends on current request scope)."""
raise NotImplementedError()
@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 NoneView on GitHub (pinned to 0d6fbdeffa)