{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/fixtures.py#L573-L609","documentation":"Raised by `request.cls` when the request scope is neither \"class\" nor \"function\". The class that collected the test is only meaningful inside class- or function-scoped requests; session/module/package requests have no associated class, so pytest raises AttributeError. `request.cls` may also legitimately return None for non-class tests.","triggerScenarios":"Calling `request.cls` inside a fixture with `scope=\"session\"`, `\"module\"`, or `\"package\"`. e.g. `@pytest.fixture(scope=\"module\")\\ndef cfg(request):\\n    if request.cls is not None: ...` raises instead of returning None.","commonSituations":"Widening a class-aware fixture's scope for performance while still branching on `request.cls`. Sharing a fixture across modules via session scope but leaving class introspection in.","solutions":["Guard the access with a scope check: `if request.scope in (\"class\", \"function\") and request.cls is not None:`.","Split into a function/class-scoped fixture that reads `request.cls` and feeds the wider-scoped fixture.","Use `request.node.getparent(pytest.Class)` to find a class node without the scope restriction."],"exampleFix":"// before\n@pytest.fixture(scope=\"session\")\ndef cfg(request):\n    cls = request.cls  # AttributeError\n// after\n@pytest.fixture(scope=\"session\")\ndef cfg(request):\n    cls = None\n    if request.scope in (\"class\", \"function\"):\n        cls = request.cls","handlingStrategy":"validation","validationCode":"if request.scope in (\"class\", \"function\"):\n    cls = request.cls\nelse:\n    cls = None","typeGuard":"def can_access_cls(request) -> bool:\n    return getattr(request, \"scope\", None) in (\"class\", \"function\")","tryCatchPattern":"try:\n    cls = request.cls\nexcept AttributeError:\n    cls = None","preventionTips":["Guard request.cls with a scope check; it may also legitimately be None for non-class tests.","Use request.node.getparent(pytest.Class) to find a class without scope restrictions."],"tags":["pytest","fixtures","scope","attributeerror"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}