{"id":"98e1b970a68f6922","repo":"pytest-dev/pytest","slug":"path-not-available-in-self-scope-scoped-context","errorCode":null,"errorMessage":"path not available in {self.scope}-scoped context","messagePattern":"path not available in (.+?)-scoped context","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":616,"sourceCode":"        \"\"\"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\n    def session(self) -> Session:\n        \"\"\"Pytest session object.\"\"\"\n        return self._pyfuncitem.session\n\n    @abc.abstractmethod\n    def addfinalizer(self, finalizer: Callable[[], object]) -> None:\n        \"\"\"Add finalizer/teardown function to be called without arguments after\n        the last test within the requesting test context finished execution.\"\"\"\n        raise NotImplementedError()","sourceCodeStart":598,"sourceCodeEnd":634,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/fixtures.py#L598-L634","documentation":"Raised by `request.path` when the request scope is not in (\"function\", \"class\", \"module\", \"package\"). Only the session scope (and beyond) lacks a concrete filesystem path, so this fires almost exclusively from session-scoped fixtures. The path returned otherwise is the file where the test was collected.","triggerScenarios":"Calling `request.path` inside a fixture with `scope=\"session\"`. e.g. `@pytest.fixture(scope=\"session\")\\ndef repo_root(request):\\n    return request.path.parent.parent`.","commonSituations":"A session-scoped fixture in conftest.py trying to derive paths relative to the test file. Refactoring a function-scoped path-based fixture to session scope.","solutions":["Use `request.config.rootpath` for repo-root-relative resolution in session scope.","Derive the path from a collected node via `request.session` items, or pass it in from a narrower fixture.","Keep the fixture at module/package scope if a per-file path is required."],"exampleFix":"// before\n@pytest.fixture(scope=\"session\")\ndef base(request):\n    return request.path  # AttributeError\n// after\n@pytest.fixture(scope=\"session\")\ndef base(request):\n    return request.config.rootpath","handlingStrategy":"validation","validationCode":"if request.scope in (\"function\", \"class\", \"module\", \"package\"):\n    p = request.path\nelse:\n    p = request.config.rootpath  # session scope","typeGuard":"def can_access_path(request) -> bool:\n    return getattr(request, \"scope\", None) in (\"function\", \"class\", \"module\", \"package\")","tryCatchPattern":"try:\n    p = request.path\nexcept AttributeError:\n    p = request.config.rootpath","preventionTips":["Use request.config.rootpath for repo-root-relative paths in session scope.","Derive paths from a narrower fixture when per-file resolution is required."],"tags":["pytest","fixtures","scope","attributeerror","path"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}