python / python/cpython

Clarify which parameters are accepted by Traversable.open

Đang mở
#137,070 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

docs topic-importlib
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với tài liệu về Traversable.open tại trang importlib.resources.abc được liên kết, sau đó xem xét các tham số được ghi trong tài liệu và các tham số thực tế của phương thức open, đặc biệt là encoding và newline. Hãy kiểm tra cuộc thảo luận và các PR được liên kết gh-137076 và gh-157294 trước khi quyết định liệu tài liệu có nên mô tả rõ ràng newline hay không; hoàn tất khi các tham số được chấp nhận và cách sử dụng CSV dự kiến là rõ ràng, không mơ hồ.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.