{"record":{"id":"3a244824306af4a2","repo":"pytest-dev/pytest","slug":"cls-not-available-in-self-scope-scoped-context","errorCode":null,"errorMessage":"cls not available in {self.scope}-scoped context","messagePattern":"cls not available in (.+?)-scoped context","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":591,"sourceCode":"    @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\n        return getattr(self._pyfuncitem, \"instance\", None)\n\n    @property\n    def module(self):\n        \"\"\"Python module object where the test function was collected.\"\"\"\n        if self.scope not in (\"function\", \"class\", \"module\"):\n            raise AttributeError(f\"module not available in {self.scope}-scoped context\")\n        mod = self._pyfuncitem.getparent(_pytest.python.Module)\n        assert mod is not None","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/fixtures.py#L573-L609","documentation":"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.","triggerScenarios":"Calling request.cls inside a session-scoped fixture; a module-scoped fixture that tries to read the test class.","commonSituations":"Promoting a function-scoped fixture to module scope; running a fixture from a non-class test where cls would be None anyway.","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\"):'."],"exampleFix":"// before\n@pytest.fixture(scope='module')\ndef mod(request):\n    return request.cls\n// after\n@pytest.fixture\ndef per_class(request):\n    return request.cls","handlingStrategy":"type-guard","validationCode":"null","typeGuard":"def has_cls_scope(request) -> bool:\n    return getattr(request, 'scope', None) in ('class', 'function')\n\nif has_cls_scope(request):\n    cls = request.cls","tryCatchPattern":"null","preventionTips":["Restrict request.cls access to class/function scope.","Prefer explicit fixture dependencies over reaching through request."],"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"}