From 0ae2472c72c673e4905315afd845b7cb64a8b044 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 13 Oct 2018 18:07:24 -0700 Subject: [PATCH] closes #329 - string coersion is too broad causing typing errors --- gooey/python_bindings/argparse_to_json.py | 17 ++++++++++++-- gooey/tests/test_argparse_to_json.py | 28 +++++++++++------------ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index 5c763fa..c668f9b 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/gooey/python_bindings/argparse_to_json.py @@ -54,6 +54,8 @@ group_defaults = { item_default = { 'error_color': '#ea7878', + 'label_color': '#ff1111', + 'help_color': '#363636', 'validator': { 'type': 'local', 'test': 'lambda x: True', @@ -301,7 +303,7 @@ def build_radio_group(mutex_group, widget_group, options): 'cli_type': 'optional', 'group_name': 'Choose Option', 'required': mutex_group.required, - 'options': getattr(mutex_group, 'gooey_options', {}), + 'options': merge(item_default, getattr(mutex_group, 'gooey_options', {})), 'data': { 'commands': [action.option_strings for action in mutex_group._group_actions], 'widgets': list(categorize(mutex_group._group_actions, widget_group, options)) @@ -334,7 +336,7 @@ def action_to_json(action, widget, options): # Issue #321: # Defaults for choice types must be coerced to strings # to be able to match the stringified `choices` used by `wx.ComboBox` - default = (str(clean_default(action.default)) + default = (safe_string(clean_default(action.default)) if widget in dropdown_types else clean_default(action.default)) @@ -373,3 +375,14 @@ def clean_default(default): `default` parameter in Argparse cause errors in Gooey. ''' return default.__name__ if callable(default) else default + + +def safe_string(value): + """ + Coerce a type to string as long as it isn't None + """ + if value is None or isinstance(value, bool): + return value + else: + return str(value) + diff --git a/gooey/tests/test_argparse_to_json.py b/gooey/tests/test_argparse_to_json.py index ade8bcd..fe47fff 100644 --- a/gooey/tests/test_argparse_to_json.py +++ b/gooey/tests/test_argparse_to_json.py @@ -1,21 +1,7 @@ -import json -import sys -import time import unittest from argparse import ArgumentParser -from concurrent import futures -from os import path -import wx - -from gooey.gui import application -from gooey.gui.lang.i18n import _ -from gooey.gui.util.freeze import getResourcePath -from gooey.gui.util.quoting import quote -from gooey.gui.components.widgets import Dropdown -from python_bindings import argparse_to_json -import os -from pprint import pprint +from python_bindings import argparse_to_json from util.functional import getin @@ -59,5 +45,17 @@ class TestArgparse(unittest.TestCase): self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3']) # default value is also converted to a string type self.assertEqual(getin(result, ['data', 'default']), '1') + + def test_choice_string_cooersion_no_default(self): + """ + Make sure that choice types without a default don't create + the literal string "None" but stick with the value None + """ + 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', 'default']), None)