Clarify which parameters are accepted by Traversable.open
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Dokumentation zu Traversable.open auf der verlinkten Seite importlib.resources.abc und überprüfe anschließend die dokumentierten und tatsächlichen Parameter der open-Methode, insbesondere encoding und newline. Sieh dir die Diskussion und die verlinkten PRs gh-137076 und gh-157294 an, bevor du entscheidest, ob die Dokumentation newline ausdrücklich beschreiben sollte; fertig ist die Aufgabe, wenn die akzeptierten Parameter und die vorgesehene CSV-Nutzung eindeutig sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100