Clarify which parameters are accepted by Traversable.open
還沒有人認領這個 Issue。
- 主要語言
- 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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 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