This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: csv.DictWriter: inconsistency in handling of extrasaction arg
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk
Priority: normal Keywords: patch

Created on 2021-06-26 02:32 by andrei.avk, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 26924 open andrei.avk, 2021-06-27 16:11
Messages (1)
msg396536 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-06-26 02:32
In csv.DictWriter, the arg `extrasaction` case is handled inconsistently:

- if it's misspelled, e.g. ignor instead of 'ignore', a ValueError is raised by the __init__ method.

- if it's 'Ignore', 'IGNORE', it will work properly

- if it's 'raise', it will also work properly

- BUT: if it's 'Raise' or 'RAISE', it will have the same effect as 'ignore'

The code is here:

[ins] In [1]: from csv import DictWriter

[ins] In [4]: d=DictWriter(open('testcsv','w'),'abc',extrasaction='IGNORE')

[ins] In [5]: list(d._dict_to_list(dict(d=1)))
Out[6]: ['', '', '']

[ins] In [7]: d=DictWriter(open('testcsv','w'),'abc',extrasaction='RAISE')

[ins] In [8]: list( d._dict_to_list(dict(d=1)) )
Out[8]: ['', '', '']

[ins] In [9]: d=DictWriter(open('testcsv','w'),'abc',extrasaction='TEST')
ValueError                                Traceback (most recent call last)
<ipython-input-9-953d1100c7f9> in <module>
----> 1 d=DictWriter(open('testcsv','w'),'abc',extrasaction='TEST')

/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ in __init__(self, f, fieldnames, restval, extrasaction, dialect, *args, **kwds)
    134         self.restval = restval          # for writing short dicts
    135         if extrasaction.lower() not in ("raise", "ignore"):
--> 136             raise ValueError("extrasaction (%s) must be 'raise' or 'ignore'"
    137                              % extrasaction)
    138         self.extrasaction = extrasaction

ValueError: extrasaction (TEST) must be 'raise' or 'ignore'

[ins] In [10]: d=DictWriter(open('testcsv','w'),'abc',extrasaction='raise')

[ins] In [11]: list( d._dict_to_list(dict(d=1)) )
ValueError                                Traceback (most recent call last)
<ipython-input-11-1fde207e1805> in <module>
----> 1 list( d._dict_to_list(dict(d=1)) )

/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ in _dict_to_list(self, rowdict)
    147             wrong_fields = rowdict.keys() - self.fieldnames
    148             if wrong_fields:
--> 149                 raise ValueError("dict contains fields not in fieldnames: "
    150                                  + ", ".join([repr(x) for x in wrong_fields]))
    151         return (rowdict.get(key, self.restval) for key in self.fieldnames)

ValueError: dict contains fields not in fieldnames: 'd'

I propose to accept any case of Raise, RAISE, etc, to have the effect of 'raise'.

I can put up the PR if that sounds good.
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88678
2021-06-27 16:11:26andrei.avksetkeywords: + patch
stage: patch review
pull_requests: + pull_request25496
2021-06-26 02:32:57andrei.avkcreate