{"record":{"id":"e2cb6adfc430c2c5","repo":"pytest-dev/pytest","slug":"function-not-available-in-self-scope-scoped-cont","errorCode":null,"errorMessage":"function not available in {self.scope}-scoped context","messagePattern":"function not available in (.+?)-scoped context","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":582,"sourceCode":"        result.extend(set(self._fixture_defs).difference(result))\n        return result\n\n    @property\n    @abc.abstractmethod\n    def node(self):\n        \"\"\"Underlying collection node (depends on current request scope).\"\"\"\n        raise NotImplementedError()\n\n    @property\n    def config(self) -> Config:\n        \"\"\"The pytest config object associated with this request.\"\"\"\n        return self._pyfuncitem.config\n\n    @property\n    def function(self):\n        \"\"\"Test function object if the request has a per-function scope.\"\"\"\n        if self.scope != \"function\":\n            raise AttributeError(\n                f\"function not available in {self.scope}-scoped context\"\n            )\n        return self._pyfuncitem.obj\n\n    @property\n    def cls(self):\n        \"\"\"Class (can be None) where the test function was collected.\"\"\"\n        if self.scope not in (\"class\", \"function\"):\n            raise AttributeError(f\"cls not available in {self.scope}-scoped context\")\n        clscol = self._pyfuncitem.getparent(_pytest.python.Class)\n        if clscol:\n            return clscol.obj\n\n    @property\n    def instance(self):\n        \"\"\"Instance (can be None) on which test function was collected.\"\"\"\n        if self.scope != \"function\":\n            return None","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/fixtures.py#L564-L600","documentation":"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.","triggerScenarios":"Calling request.function inside a session- or module-scoped fixture; using request.getfixturevalue('request').function in setup of a class-scoped fixture.","commonSituations":"Refactoring a function-scoped fixture to module scope without removing request.function usage; copying fixture code into a conftest session fixture.","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."],"exampleFix":"// before\n@pytest.fixture(scope='session')\ndef sess(request):\n    return request.function.__name__\n// after\n@pytest.fixture(scope='session')\ndef sess():\n    return 'session-wide'\n\n@pytest.fixture\ndef per_test(request, sess):\n    return request.function.__name__","handlingStrategy":"type-guard","validationCode":"null","typeGuard":"def is_function_scope(request) -> bool:\n    return getattr(request, 'scope', None) == 'function'\n\n# usage\nif is_function_scope(request):\n    fn = request.function","tryCatchPattern":"null","preventionTips":["Keep request.function access inside function-scoped fixtures.","When widening a fixture's scope, audit every request.* access."],"tags":["fixtures","scope","request"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}