{"id":"90821d20c3adf395","repo":"pytest-dev/pytest","slug":"name-is-not-allowed-to-contain-path-separators","errorCode":null,"errorMessage":"name is not allowed to contain path separators","messagePattern":"name is not allowed to contain path separators","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/cacheprovider.py","lineNumber":179,"sourceCode":"        path.mkdir(exist_ok=True, parents=True)\n\n    def mkdir(self, name: str) -> Path:\n        \"\"\"Return a directory path object with the given name.\n\n        If the directory does not yet exist, it will be created. You can use\n        it to manage files to e.g. store/retrieve database dumps across test\n        sessions.\n\n        .. versionadded:: 7.0\n\n        :param name:\n            Must be a string not containing a ``/`` separator.\n            Make sure the name contains your plugin or application\n            identifiers to prevent clashes with other cache users.\n        \"\"\"\n        path = Path(name)\n        if len(path.parts) > 1:\n            raise ValueError(\"name is not allowed to contain path separators\")\n        res = self._cachedir.joinpath(self._CACHE_PREFIX_DIRS, path)\n        self._mkdir(res)\n        return res\n\n    def _getvaluepath(self, key: str) -> Path:\n        return self._cachedir.joinpath(self._CACHE_PREFIX_VALUES, Path(key))\n\n    def get(self, key: str, default):\n        \"\"\"Return the cached value for the given key.\n\n        If no value was yet cached or the value cannot be read, the specified\n        default is returned.\n\n        :param key:\n            Must be a ``/`` separated value. Usually the first\n            name is the name of your plugin or your application.\n        :param default:\n            The value to return in case of a cache-miss or invalid cache value.","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/cacheprovider.py#L161-L197","documentation":"The Cache.mkdir method creates a directory under pytest's cache for storing files across sessions. The name parameter must be a simple name without path separators. If Path(name).parts has more than one element (i.e., contains '/' or '\\'), pytest raises ValueError to prevent path traversal and keep the cache layout flat.","triggerScenarios":"Calling request.config.cache.mkdir('subdir/mydir') or cache.mkdir('a/b/c'). Path('subdir/mydir').parts yields ('subdir', 'mydir'), length 2 > 1, so ValueError is raised.","commonSituations":"Trying to organize cached files into a nested directory hierarchy, or accidentally including an absolute path or leading './' in the name.","solutions":["Use a flat, separator-free name: cache.mkdir('myplugin_data').","Namespace the name with a prefix instead of a path: cache.mkdir('myplugin_data').","If you need nested storage, manage paths manually under the returned directory."],"exampleFix":"# before\nrequest.config.cache.mkdir('myplugin/output')\n\n# after\nrequest.config.cache.mkdir('myplugin_output')","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef safe_cache_mkdir(cache, name: str):\n    if len(Path(name).parts) > 1:\n        raise ValueError(f\"Cache mkdir name must not contain path separators, got: {name!r}\")\n    return cache.mkdir(name)","typeGuard":"from pathlib import Path\n\ndef is_flat_cache_name(name: str) -> bool:\n    return isinstance(name, str) and len(Path(name).parts) == 1","tryCatchPattern":null,"preventionTips":["Use flat, descriptive names for cache directories (e.g., 'myplugin_dump').","Prefix names with your plugin/app identifier to avoid collisions.","Strip any leading './' or '/' from names before calling cache.mkdir()."],"tags":["api","cache","filesystem","value-error","validation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}