-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Description
Bug report
Bug description:
Using pathlib
with shutil
usually works, but there are some quirks when it comes to return types. As an example, see the copy
function (but other functions are affected as well):
Lines 468 to 484 in 67ded6a
def copy(src, dst, *, follow_symlinks=True): | |
"""Copy data and mode bits ("cp src dst"). Return the file's destination. | |
The destination may be a directory. | |
If follow_symlinks is false, symlinks won't be followed. This | |
resembles GNU's "cp -P src dst". | |
If source and destination are the same file, a SameFileError will be | |
raised. | |
""" | |
if os.path.isdir(dst): | |
dst = os.path.join(dst, os.path.basename(src)) | |
copyfile(src, dst, follow_symlinks=follow_symlinks) | |
copymode(src, dst, follow_symlinks=follow_symlinks) | |
return dst |
In this case, if a Path
object gets passed in as dst
, it will be returned unchanged if it is not a directory, but a str
will be returned if it is a directory. That's unexpected, the return type should be consistent, probably by always returning a str
.
I assume that pathlib support is mostly incidental in shutil, considering that the latter predates the former. shutil should probably be reviewed for proper pathlib support at some point. (See also a few other pathlib/shutil related issues reported here.)
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response