csv.Dialect: Defaults are documented but not correctly implemented

Open
#100,335 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
data

Research direction

Start with the csv.py entry points for csv.Dialect and csv.excel, then run the reproducer from the issue to observe the current defaults and error. Done means the documented Dialect defaults behave as described without breaking existing csv behavior; gh-100336 is already linked as work on this issue.

Written by the indexing model from the issue text.

Description

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
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

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.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.