{"record":{"id":"25597b2d15ef2b22","repo":"pytest-dev/pytest","slug":"can-only-pass-none-path-instances-or-non-empty-st","errorCode":null,"errorMessage":"can only pass None, Path instances or non-empty strings to LocalPath","messagePattern":"can only pass None, Path instances or non-empty strings to LocalPath","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/_py/path.py","lineNumber":291,"sourceCode":"    sep = os.sep\n\n    def __init__(self, path=None, expanduser=False):\n        \"\"\"Initialize and return a local Path instance.\n\n        Path can be relative to the current directory.\n        If path is None it defaults to the current working directory.\n        If expanduser is True, tilde-expansion is performed.\n        Note that Path instances always carry an absolute path.\n        Note also that passing in a local path object will simply return\n        the exact same path object. Use new() to get a new copy.\n        \"\"\"\n        if path is None:\n            self.strpath = error.checked_call(os.getcwd)\n        else:\n            try:\n                path = os.fspath(path)\n            except TypeError:\n                raise ValueError(\n                    \"can only pass None, Path instances \"\n                    \"or non-empty strings to LocalPath\"\n                )\n            if expanduser:\n                path = os.path.expanduser(path)\n            self.strpath = abspath(path)\n\n    if sys.platform != \"win32\":\n\n        def chown(self, user, group, rec=0):\n            \"\"\"Change ownership to the given user and group.\n            user and group may be specified by a number or\n            by a name.  if rec is True change ownership\n            recursively.\n            \"\"\"\n            uid = getuserid(user)\n            gid = getgroupid(group)\n            if rec:","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/_py/path.py#L273-L309","documentation":"LocalPath.__init__ converts its argument via os.fspath, which only accepts str, bytes, or os.PathLike objects. If fspath raises TypeError (the argument is an int, list, None-other, dict, arbitrary object), pytest re-raises it as ValueError with this message. LocalPath deliberately narrows the accepted types to path-like objects and the None sentinel (which means cwd).","triggerScenarios":"Calling LocalPath(123), LocalPath([]), LocalPath(None, ...) is fine (None means cwd), LocalPath(SomeRandomObject()). Triggered at LocalPath.__init__ (src/_pytest/_py/path.py:288-294). Note: bytes paths ARE accepted by os.fspath, but the message wording focuses on Path/string.","commonSituations":"Passing a numeric identifier where a path was expected; passing a list of path components instead of using .join; passing a configuration object that wraps a path but is not os.PathLike.","solutions":["Pass a str or os.PathLike: `LocalPath('/tmp/x')` or `LocalPath(pathlib.Path('/tmp/x'))`.","If you have components, join first: `LocalPath(os.path.join(*components))`.","Implement os.fspath/__fspath__ on your wrapper class so it is path-like."],"exampleFix":"# before\nLocalPath(12345)\n# after\nLocalPath('/tmp/dir_12345')","handlingStrategy":"type-guard","validationCode":"import os\ndef is_path_like(v) -> bool:\n    return v is None or isinstance(v, (str, bytes, os.PathLike))\n# usage\nif not is_path_like(arg):\n    raise TypeError('LocalPath requires str/bytes/os.PathLike or None')\nLocalPath(arg)","typeGuard":"import os\ndef is_path_like_or_none(v) -> bool:\n    return v is None or isinstance(v, (str, bytes, os.PathLike))","tryCatchPattern":null,"preventionTips":["Only pass str, bytes, os.PathLike, or None to LocalPath.","Join path components with os.path.join before constructing.","Implement __fspath__ on wrapper classes that should be path-like."],"tags":["pytest","localpath","py-path","type-validation","legacy"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}