Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

shutil: Inconsistent return types when using pathlib #132322

Open
Labels
stdlibPython modules in the Lib dirtopic-pathlibtype-bugAn unexpected behavior, bug, or error
@srittau

Description

@srittau

Bug report

Bug description:

Usingpathlib withshutil usually works, but there are some quirks when it comes to return types. As an example, see thecopy function (but other functions are affected as well):

cpython/Lib/shutil.py

Lines 468 to 484 in67ded6a

defcopy(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.
"""
ifos.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)
returndst

In this case, if aPath object gets passed in asdst, it will be returned unchanged if it is not a directory, but astr will be returned if it is a directory. That's unexpected, the return type should be consistent, probably by always returning astr.

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

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-pathlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp