{"record":{"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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/fixtures.py#L598-L634","documentation":"request.path returns the filesystem path where the test was collected, available in 'function', 'class', 'module', and 'package' scopes. In a session scope there is no single path, so pytest raises AttributeError naming 'session' as the scope.","triggerScenarios":"Calling request.path from a session-scoped fixture; an autouse session fixture reaching for the test file path.","commonSituations":"Promoting a fixture to session scope; writing session-level setup that assumed per-file context.","solutions":["Move the request.path access into a function/class/module/package-scoped fixture.","If you need the path in a wider scope, depend on a narrower fixture that passes the path up.","Guard access with 'if request.scope in (\"function\", \"class\", \"module\", \"package\"):'."],"exampleFix":"// before\n@pytest.fixture(scope='session')\ndef sess(request):\n    return request.path\n// after\n@pytest.fixture\ndef per_test(request):\n    return request.path","handlingStrategy":"type-guard","validationCode":"null","typeGuard":"def has_path_scope(request) -> bool:\n    return getattr(request, 'scope', None) in ('function', 'class', 'module', 'package')\n\nif has_path_scope(request):\n    p = request.path","tryCatchPattern":"null","preventionTips":["Do not call request.path from session-scoped fixtures.","Access path in a narrower fixture and pass it up if needed."],"tags":["fixtures","scope","request","path"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}