From 756ab67a9867cd158aa63d9ac5ce99809e0a2175 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 2 Apr 2009 22:25:40 +0000 Subject: Have namedtuple's field renamer assign names that are consistent with the corresponding tuple index. --- Lib/collections.py | 2 +- Lib/test/test_collections.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/collections.py b/Lib/collections.py index d77aff5..fb53900 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -178,7 +178,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False): if (not all(c.isalnum() or c=='_' for c in name) or _iskeyword(name) or not name or name[0].isdigit() or name.startswith('_') or name in seen): - names[i] = '_%d' % (i+1) + names[i] = '_%d' % i seen.add(name) field_names = tuple(names) for name in (typename,) + field_names: diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 8ffa75b..f822b6b 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -49,12 +49,12 @@ class TestNamedTuple(unittest.TestCase): def test_name_fixer(self): for spec, renamed in [ - [('efg', 'g%hi'), ('efg', '_2')], # field with non-alpha char - [('abc', 'class'), ('abc', '_2')], # field has keyword - [('8efg', '9ghi'), ('_1', '_2')], # field starts with digit - [('abc', '_efg'), ('abc', '_2')], # field with leading underscore - [('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_3', 'ghi')], # duplicate field - [('abc', '', 'x'), ('abc', '_2', 'x')], # fieldname is a space + [('efg', 'g%hi'), ('efg', '_1')], # field with non-alpha char + [('abc', 'class'), ('abc', '_1')], # field has keyword + [('8efg', '9ghi'), ('_0', '_1')], # field starts with digit + [('abc', '_efg'), ('abc', '_1')], # field with leading underscore + [('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_2', 'ghi')], # duplicate field + [('abc', '', 'x'), ('abc', '_1', 'x')], # fieldname is a space ]: self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed) -- cgit v0.12