python / python/cpython

shutil: Inconsistent return types when using pathlib

未關閉
#132,322 3 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib topic-pathlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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):

https://github.com/python/cpython/blob/67ded6a4faae29edff8e4f7886978e71ce116e33/Lib/shutil.py#L468-L484

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

Linked PRs
  • gh-132820

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先檢查連結的 PR gh-132820,然後檢視 Lib/shutil.py 中 copy 附近的程式碼,以及報告中提到的其他 pathlib 相關函式。檢查現有的 shutil 測試,並確保受影響的函式具有一致且有文件說明的回傳型別,同時使用測試涵蓋作為目錄和不作為目錄的 Path 目的地。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。