From: Raymond Hettinger Date: Mon, 21 May 2007 16:40:10 +0000 (+0000) Subject: Allow all alphanumeric and underscores in type and field names. X-Git-Tag: v2.6a1~1711 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=767debb6aa5729e919da309ecc770f8b2d94beba;p=python Allow all alphanumeric and underscores in type and field names. --- diff --git a/Lib/collections.py b/Lib/collections.py index a2ce552ba2..4a860dd0c0 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -24,7 +24,7 @@ def NamedTuple(typename, s): """ field_names = s.split() - if not ''.join([typename] + field_names).replace('_', '').isalpha(): + if not ''.join([typename] + field_names).replace('_', '').isalnum(): raise ValueError('Type names and field names can only contain alphanumeric characters and underscores') argtxt = ', '.join(field_names) reprtxt = ', '.join('%s=%%r' % name for name in field_names) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index b3f460e70e..f5dad7d223 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -11,6 +11,9 @@ class TestNamedTuple(unittest.TestCase): self.assertEqual(Point.__slots__, ()) self.assertEqual(Point.__module__, __name__) self.assertEqual(Point.__getitem__, tuple.__getitem__) + self.assertRaises(ValueError, NamedTuple, 'abc%', 'def ghi') + self.assertRaises(ValueError, NamedTuple, 'abc', 'def g%hi') + NamedTuple('Point0', 'x1 y2') # Verify that numbers are allowed in names def test_instance(self): Point = NamedTuple('Point', 'x y')