python / python/cpython

Clarify which parameters are accepted by Traversable.open

未关闭
#137,070 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

docs topic-importlib
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Documentation

The docs for the open method of the importlib.resources.abc.Traversable protocol state:

When opening as text, accepts encoding parameters such as those accepted by io.TextIOWrapper.

It would be helpful to clarify exactly which parameters are accepted here. In particular, is the newline parameter covered by this? That parameter is accepted by io.TextIOWrapper, but is arguably not an "encoding parameter", so it's unclear whether it's supported.

An aside: typeshed does not seem to think that newline is supported here, but I'm wondering whether that might be a defect in typeshed. If the consensus here is that newline is supported, I'll open a typeshed issue.

The precise case I ran into was reading CSV data from a package resource. For portable CSV file reading, using newline="" is recommended, so I'm doing:

with csv_data.open(newline="") as f:
    reader = csv.DictReader(f)
    for row in reader:
        ... #  do something with row

where csv_data implements importlib.resources.abc.Traversable. The open call is flagged as invalid by mypy and pyright, but the code appears to work correctly (including in the case where the resource is zipfile-based).

If newline is not intended to be accepted here, then I guess I instead have to do:

with importlib.resources.as_file(csv_data) as csv_file:
    with csv_file.open(newline="") as f:
        reader = csv.DictReader(f)
        for row in reader:
            ... #  do something with row

That's fine, but it would be cleaner to be able to stream data straight from the resource without having to materialise the resource as a file first.

Linked PRs
  • gh-137076
  • gh-157294

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从链接的 importlib.resources.abc 页面上的 Traversable.open 文档开始,然后检查 open 方法文档中记录的参数和实际参数,尤其是 encoding 和 newline。在决定文档是否应明确描述 newline 之前,查看讨论以及链接的 PR gh-137076 和 gh-157294;当可接受的参数和预期的 CSV 用法都明确无歧义时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。