Kaydet (Commit) 767debb6 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Allow all alphanumeric and underscores in type and field names.

üst 0d6a8ccf
...@@ -24,7 +24,7 @@ def NamedTuple(typename, s): ...@@ -24,7 +24,7 @@ def NamedTuple(typename, s):
""" """
field_names = s.split() 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') raise ValueError('Type names and field names can only contain alphanumeric characters and underscores')
argtxt = ', '.join(field_names) argtxt = ', '.join(field_names)
reprtxt = ', '.join('%s=%%r' % name for name in field_names) reprtxt = ', '.join('%s=%%r' % name for name in field_names)
......
...@@ -11,6 +11,9 @@ class TestNamedTuple(unittest.TestCase): ...@@ -11,6 +11,9 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(Point.__slots__, ()) self.assertEqual(Point.__slots__, ())
self.assertEqual(Point.__module__, __name__) self.assertEqual(Point.__module__, __name__)
self.assertEqual(Point.__getitem__, tuple.__getitem__) 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): def test_instance(self):
Point = NamedTuple('Point', 'x y') Point = NamedTuple('Point', 'x y')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment