From aeb8f67b928fc38dac0970111ba578bb146264d5 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 19 Sep 2018 20:06:58 -0700 Subject: [PATCH] closes issue #321 - coerce choices to string --- gooey/python_bindings/argparse_to_json.py | 2 +- gooey/tests/test_argparse_to_json.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index ea8fb4f..c1dd7a7 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/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, }, diff --git a/gooey/tests/test_argparse_to_json.py b/gooey/tests/test_argparse_to_json.py index a6b6a83..978dbc1 100644 --- a/gooey/tests/test_argparse_to_json.py +++ b/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']) +