python / python/cpython

Clarify which parameters are accepted by Traversable.open

Ouverte
#137,070 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

docs topic-importlib
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la documentation de Traversable.open sur la page importlib.resources.abc liée, puis examinez les paramètres documentés et réels de la méthode open, en particulier encoding et newline. Consultez la discussion et les PRs liés gh-137076 et gh-157294 avant de décider si la documentation doit décrire explicitement newline ; le travail est terminé lorsque les paramètres acceptés et l’utilisation prévue de CSV sont sans ambiguïté.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
documentation
Type d'issue
Documentation
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.