csv.Dialect: Defaults are documented but not correctly implemented

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

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

Đánh giá

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

Hướng nghiên cứu

Bắt đầu với các entry point trong csv.py cho csv.Dialect và csv.excel, sau đó chạy reproducer từ issue để quan sát các giá trị mặc định hiện tại và lỗi. Hoàn thành có nghĩa là các giá trị mặc định của Dialect được ghi trong tài liệu hoạt động như mô tả mà không làm hỏng hành vi hiện có của csv; gh-100336 đã được liên kết là công việc của issue này.

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

Mô tả

stdlib type-bug

Bug report

In gh-68335, it was reported that the default values for csv.Dialect were not documented properly. They are, as pointed out by @smontanaro there, at https://docs.python.org/2/library/csv.html#dialects-and-formatting-parameters -- and still are for Python 3.11.

However, while I was attempting to fix gh-73138, I discovered that all arguments of csv.Dialect are None and even trigger unexpected behavior (if following the documentation):

import csv
class mydialect(csv.Dialect):
    quotechar = ":"
mydialect()

raises a _csv.Error caused by a TypeError, instead of using the documented default delimiter ,.

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/csv.py", line 49, in _validate
    _Dialect(self)
TypeError: "delimiter" must be string, not NoneType

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/csv.py", line 45, in __init__
    self._validate()
  File "/opt/homebrew/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/csv.py", line 52, in _validate
    raise Error(str(e))
_csv.Error: "delimiter" must be string, not NoneType

The discrepancy is, that the documented default values are not for the csv.Dialect class, but for its child csv.excel.
In gh-96842, a fix was prepared, improving the documentation to point this out.
I think a better fix would be to actually add the proper default values as they are written in the documentation.
The updated documentation in gh-96842 might be easily missed, and developers implementing a csv.Dialect would expect the defaults to be as documented, not as the csv.excel has them (if they want to inherit from csv.excel, they can still do so explicitly).

Your environment

  • CPython versions tested on: Python 3.10, 3.11, and from the main branch `Python 3.12.0a3+
  • Operating system and architecture: MacOS arm
Linked PRs
  • gh-100336
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
558

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.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

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.