Browse Source

closes issue #321 - coerce choices to string

pull/473/head
Chris 6 years ago
parent
commit
aeb8f67b92
2 changed files with 17 additions and 5 deletions
  1. 2
      gooey/python_bindings/argparse_to_json.py
  2. 20
      gooey/tests/test_argparse_to_json.py

2
gooey/python_bindings/argparse_to_json.py

@ -342,7 +342,7 @@ def action_to_json(action, widget, options):
'required': action.required,
'nargs': action.nargs or '',
'commands': action.option_strings,
'choices': list(action.choices) if action.choices else [],
'choices': list(map(str, action.choices)) if action.choices else [],
'default': clean_default(action.default),
'dest': action.dest,
},

20
gooey/tests/test_argparse_to_json.py

@ -31,19 +31,31 @@ class TestArgparse(unittest.TestCase):
"""
# our original functionality accepted only lists as the choices arg
parser = ArgumentParser()
parser.add_argument("-foo", choices=[1, 2, 3])
parser.add_argument("-foo", choices=['foo','bar', 'baz'])
result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
choices = result['data']['choices']
self.assertTrue(isinstance(choices, list))
self.assertEqual(choices, [1, 2, 3])
self.assertEqual(choices, ['foo','bar', 'baz'])
# Now we allow tuples as well.
parser = ArgumentParser()
parser.add_argument("-foo", choices=(1, 2, 3))
parser.add_argument("-foo", choices=('foo','bar', 'baz'))
result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
choices = result['data']['choices']
self.assertTrue(isinstance(choices, list))
self.assertEqual(choices, [1, 2, 3])
self.assertEqual(choices, ['foo','bar', 'baz'])
def test_choice_string_cooersion(self):
"""
Issue 321 - must coerce choice types to string to support wx.ComboBox
"""
parser = ArgumentParser()
parser.add_argument('--foo', choices=[1, 2, 3])
choice_action = parser._actions[-1]
result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3'])
Loading…
Cancel
Save