python / python/cpython

Clarify which parameters are accepted by Traversable.open

Open
#137,070 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs topic-importlib
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Traversable.open documentation at the linked importlib.resources.abc page, then review the open method's documented and actual parameters, especially encoding and newline. Check the discussion and linked PRs gh-137076 and gh-157294 before deciding whether the documentation should explicitly describe newline; done means the accepted parameters and intended CSV usage are unambiguous.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.