classification
Title: namedtuple does not normalize field names when checking for duplicates
Type: Stage:
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Jim.Jewett, rhettinger
Priority: low Keywords:

Created on 2012-01-26 05:47 by Jim.Jewett, last changed 2012-01-26 07:54 by rhettinger.

Messages (2)
msg151997 - (view) Author: Jim Jewett (Jim.Jewett) Date: 2012-01-26 05:47
collections.namedtuple raises a ValueError if any of the field names are not valid identifiers, or are duplicates.

It does not normalize the identifiers when checking for duplicates.

(Similar issue with the typename)

>>> namedtuple("dup_fields", ["a", "a"])
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    namedtuple("dup_fields", ["a", "a"])
  File "C:\python32\lib\collections.py", line 345, in namedtuple
    raise ValueError('Encountered duplicate field name: %r' % name)
ValueError: Encountered duplicate field name: 'a'



>>> namedtuple("nfk_tester", ["a", "ª"])
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    namedtuple("nfk_tester", ["a", "ª"])
  File "C:\python32\lib\collections.py", line 365, in namedtuple
    raise SyntaxError(e.msg + ':\n\n' + class_definition)
  File "<string>", line None
SyntaxError: duplicate argument 'a' in function definition:
...



and 


>>> namedtuple("justª", "ª")
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    namedtuple("justª", "ª")
  File "C:\python32\lib\collections.py", line 366, in namedtuple
    result = namespace[typename]
KeyError: 'justª'
msg151998 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-01-26 07:48
The SyntaxError is fine.  The dupcheck isn't about sanitization anyway; rather, it was part of a suite of tests designed to add a more helpful error messages than the usual ones you get if you had rolled a class by hand.  I would close as "invalid" but want to think it over for a while -- we'll see how all your other normalization "bugs" resolve.
History
Date User Action Args
2012-01-26 07:54:29rhettingersettitle: namedtuple does not normalize field names when sanitizing -> namedtuple does not normalize field names when checking for duplicates
2012-01-26 07:48:55rhettingersetmessages: + msg151998
2012-01-26 07:32:18rhettingersetpriority: normal -> low
assignee: rhettinger

nosy: + rhettinger
2012-01-26 05:47:41Jim.Jewettcreate