Make the error messages more specific

This commit is contained in:
Raymond Hettinger 2007-10-09 01:36:23 +00:00
parent 4b3074c795
commit 163f622c03
1 changed files with 9 additions and 6 deletions

View File

@ -34,12 +34,15 @@ def named_tuple(typename, field_names, verbose=False):
field_names = tuple(field_names) field_names = tuple(field_names)
if not ''.join((typename,) + field_names).replace('_', '').isalnum(): 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')
if any(name.startswith('__') and name.endswith('__') for name in field_names): seen_names = set()
raise ValueError('Field names cannot start and end with double underscores') for name in field_names:
if any(name[:1].isdigit() for name in field_names): if name.startswith('__') and name.endswith('__'):
raise ValueError('Field names cannot start with a number') raise ValueError('Field names cannot start and end with double underscores: %s' % name)
if len(field_names) != len(set(field_names)): if name[:1].isdigit():
raise ValueError('Encountered duplicate field name') raise ValueError('Field names cannot start with a number: %s' % name)
if name in seen_names:
raise ValueError('Encountered duplicate field name: %s' % name)
seen_names.add(name)
# Create and fill-in the class template # Create and fill-in the class template
argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes