{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/fixtures.py#L564-L600","documentation":"Raised by the `request.function` property on a FixtureRequest/SubRequest when the request's scope is not \"function\". pytest only knows the underlying test function object during a function-scoped setup, so accessing `request.function` from a session/module/class/package-scoped fixture is undefined and is rejected with an AttributeError. The scope is fixed when the fixture is declared via `@pytest.fixture(scope=...)`.","triggerScenarios":"Accessing `request.function` inside a fixture declared with `scope=\"session\"`, `\"module\"`, `\"class\"`, or `\"package\"`. For example `@pytest.fixture(scope=\"session\")\\ndef db(request):\\n    test_name = request.function.__name__` raises this because session scope has no single function bound to the request.","commonSituations":"Promoting a function-scoped helper fixture to a wider scope (session/module) to share state, while leaving `request.function`/`request.instance` accesses in place. Copy-pasting fixture code that worked at function scope into a session-scoped conftest.py fixture. Plugins that introspect the running test.","solutions":["Move the `request.function` access into a separate function-scoped fixture that depends on the wider-scoped one.","Use `request.node` (available at all scopes) and read attributes off the node, e.g. `request.node.name` or `request.node.obj`.","If you need per-test data in a session fixture, pass it via a function-scoped fixture param or `request.getfixturevalue(...)` before yield.","Reconsider the scope: if the fixture genuinely needs the test function, it must be function-scoped."],"exampleFix":"// before\n@pytest.fixture(scope=\"session\")\ndef db(request):\n    name = request.function.__name__  # AttributeError\n// after\n@pytest.fixture(scope=\"session\")\ndef db(request):\n    node = request.node  # always available\n    print(node.nodeid)","handlingStrategy":"validation","validationCode":"if request.scope == \"function\":\n    fn = request.function\nelse:\n    fn = None  # or use request.node","typeGuard":"def has_function(request) -> bool:\n    return getattr(request, \"scope\", None) == \"function\"","tryCatchPattern":"try:\n    fn = request.function\nexcept AttributeError:\n    fn = None","preventionTips":["Check request.scope before accessing request.function/cls/module/path/instance.","Keep function-specific introspection in function-scoped fixtures.","Prefer request.node for scope-agnostic access."],"tags":["pytest","fixtures","scope","attributeerror"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}