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.

Author andrei.avk
Recipients andrei.avk
Date 2021-06-26.02:32:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1624674777.38.0.50893926031.issue44512@roundup.psfhosted.org>
In-reply-to
Content
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: https://github.com/python/cpython/blob/main/Lib/csv.py#L135-L146


[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/csv.py 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/csv.py 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.
History
Date User Action Args
2021-06-26 02:32:57andrei.avksetrecipients: + andrei.avk
2021-06-26 02:32:57andrei.avksetmessageid: <1624674777.38.0.50893926031.issue44512@roundup.psfhosted.org>
2021-06-26 02:32:56andrei.avklinkissue44512 messages
2021-06-26 02:32:56andrei.avkcreate