Suffix in pathlib is not behaving like a file extension
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
According to the docs here a suffix is defined as:
The file extension of the final component, if any
But pathlib doesn't behave as expected, illustrating that on Python 3.12.4:
>>> Path("Mr. Smith resume for review").suffix
'. Smith resume for review'
This is particularly problematic for methods like with_suffix. According to the docs here that should:
Return a new path with the suffix changed. If the original path doesn’t have a suffix, the new suffix is appended instead. If the suffix is an empty string, the original suffix is removed
But as established above, that results in:
>>> Path("Mr. Smith resume for review").with_suffix(".pdf")
PosixPath('Mr.pdf')
I've characterized that as a bug as that is not working as described, but could also be a matter of documentation improvement.
I see a few ways:
- Implement
add_suffix, or an argument towith_suffix, but I know that has been discussed before and was ultimately decided against. Also this is a matter of how suffix is defined, not how it's processed. - Sanity check what a suffix can be, e.g. suffix can't have white space. But ultimately difficult without making assumption.
- Adjust the documentation: Clarify that suffix is not equal to file extension but something like last segment separated by a dot or similar. Maybe even with a warning that this can have unexpected behavior as showcased above.
CPython versions tested on:
3.12
Operating systems tested on:
Linux, Windows
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先重現 Python 3.12 中關於 pathlib 的 PurePath.suffix 和 with_suffix 範例,然後檢查 pathlib 的實作與文件,了解後綴是如何定義的。將觀察到的行為與文件中關於檔案副檔名的措辭進行比較,並判斷該問題是透過行為變更解決,還是透過澄清文件解決。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- operating-systems
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100