{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/fixtures.py#L589-L625","documentation":"Raised by `request.module` when the request scope is not one of \"function\", \"class\", or \"module\". A module object is only resolvable for scopes at-or-narrower than module scope; session and package scoped requests do not map to a single module, so pytest raises AttributeError.","triggerScenarios":"Accessing `request.module` inside a fixture declared `scope=\"session\"` or `scope=\"package\"`. e.g. a session-scoped fixture that calls `request.module.__name__` to key a cache.","commonSituations":"Caching resources per-module from a session-scoped fixture and reaching for `request.module` to build the cache key. Moving a module fixture up to session scope to avoid re-setup.","solutions":["Read the module from the node instead: `request.node.getparent(pytest.Module)` and use `.obj`.","Restrict the fixture to `scope=\"module\"` if it truly needs the module object.","Pass the module identity down from a module-scoped fixture into the session-scoped one via a parameter."],"exampleFix":"// before\n@pytest.fixture(scope=\"session\")\ndef cache(request):\n    key = request.module.__name__  # AttributeError\n// after\n@pytest.fixture(scope=\"session\")\ndef cache(request):\n    mod = request.node.getparent(pytest.Module)\n    key = mod.obj.__name__ if mod is not None else \"global\"","handlingStrategy":"validation","validationCode":"if request.scope in (\"function\", \"class\", \"module\"):\n    mod = request.module\nelse:\n    mod = request.node.getparent(_pytest.python.Module)","typeGuard":"def can_access_module(request) -> bool:\n    return getattr(request, \"scope\", None) in (\"function\", \"class\", \"module\")","tryCatchPattern":"try:\n    mod = request.module\nexcept AttributeError:\n    mod = None","preventionTips":["Restrict module introspection to module-or-narrower scopes.","Cache per-module data by reading the module from a narrower fixture."],"tags":["pytest","fixtures","scope","attributeerror"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}