{"record":{"id":"2555766fb0e0a074","repo":"pytest-dev/pytest","slug":"module-not-available-in-self-scope-scoped-contex","errorCode":null,"errorMessage":"module not available in {self.scope}-scoped context","messagePattern":"module not available in (.+?)-scoped context","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":607,"sourceCode":"        \"\"\"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\n        return mod.obj\n\n    @property\n    def path(self) -> Path:\n        \"\"\"Path where the test function was collected.\"\"\"\n        if self.scope not in (\"function\", \"class\", \"module\", \"package\"):\n            raise AttributeError(f\"path not available in {self.scope}-scoped context\")\n        return self._pyfuncitem.path\n\n    @property\n    def keywords(self) -> MutableMapping[str, Any]:\n        \"\"\"Keywords/markers dictionary for the underlying node.\"\"\"\n        node: nodes.Node = self.node\n        return node.keywords\n\n    @property","sourceCodeStart":589,"sourceCodeEnd":625,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/fixtures.py#L589-L625","documentation":"request.module returns the Python module where the test was collected, but only for 'function', 'class', or 'module' scopes. In a session or package scope there is no single module, so pytest raises AttributeError naming the offending scope.","triggerScenarios":"Calling request.module from a session-scoped fixture; a package-scoped fixture reaching for the module object.","commonSituations":"Widening a fixture's scope during refactoring; writing a session-level autouse fixture that assumed module context.","solutions":["Access request.module from within a module/class/function-scoped fixture instead.","If a wider fixture needs module-specific data, depend on a module-scoped fixture that yields it.","Guard access: 'if request.scope in (\"function\", \"class\", \"module\"):'."],"exampleFix":"// before\n@pytest.fixture(scope='session')\ndef sess(request):\n    return request.module.__name__\n// after\n@pytest.fixture(scope='module')\ndef mod(request):\n    return request.module.__name__","handlingStrategy":"type-guard","validationCode":"null","typeGuard":"def has_module_scope(request) -> bool:\n    return getattr(request, 'scope', None) in ('function', 'class', 'module')\n\nif has_module_scope(request):\n    mod = request.module","tryCatchPattern":"null","preventionTips":["Only access request.module at function/class/module scope.","Use a module-scoped fixture to pass module data upward."],"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"}