Support trailing commas in values of list type

Trailing commas in values of list type caused extra empty elements to be
appended to list values.

Strip trailing commas before spliting values to make it more robust.

Change-Id: I6f187c437ccd10779e7b91f2aa60e0487f938b30
Closes-Bug: #1758871
This commit is contained in:
Quan Tian 2018-03-26 02:23:22 -07:00
parent d2f432c2e8
commit 9fc82e985b
2 changed files with 10 additions and 1 deletions

View File

@ -534,6 +534,10 @@ class ListTypeTests(TypeTestHelper, unittest.TestCase):
'bar, baz',
'bam'])
def test_list_of_values_containing_trailing_comma(self):
self.assertConvertedValue('foo, bar, baz, ',
['foo', 'bar', 'baz'])
def test_list_of_lists(self):
self.type_instance = types.List(
types.List(types.String(), bounds=True)
@ -546,6 +550,11 @@ class ListTypeTests(TypeTestHelper, unittest.TestCase):
self.assertConvertedValue('1,2,3,5',
[1, 2, 3, 5])
def test_list_of_custom_type_containing_trailing_comma(self):
self.type_instance = types.List(types.Integer())
self.assertConvertedValue('1,2,3,5,',
[1, 2, 3, 5])
def test_tuple_of_custom_type(self):
self.type_instance = types.List(types.Integer())
self.assertConvertedValue(('1', '2', '3', '5'),

View File

@ -481,7 +481,7 @@ class List(ConfigType):
if isinstance(value, (list, tuple)):
return list(six.moves.map(self.item_type, value))
s = value.strip()
s = value.strip().rstrip(',')
if self.bounds:
if not s.startswith('['):
raise ValueError('Value should start with "["')