[issue44512] csv.DictWriter: inconsistency in handling of extrasaction arg

2021-06-27 Thread Andrei Kulakov


Change by Andrei Kulakov :


--
keywords: +patch
pull_requests: +25496
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/26924

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44512] csv.DictWriter: inconsistency in handling of extrasaction arg

2021-06-25 Thread Andrei Kulakov


New submission from Andrei Kulakov :

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')
---
ValueErrorTraceback (most recent call last)
 in 
> 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)) )
---
ValueErrorTraceback (most recent call last)
 in 
> 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.

--
components: Library (Lib)
messages: 396536
nosy: andrei.avk
priority: normal
severity: normal
status: open
title: csv.DictWriter: inconsistency in handling of extrasaction arg
versions: Python 3.10, Python 3.11, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com