Suffix in pathlib is not behaving like a file extension
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- 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