From a5ca58c60f39848ca6ed9b7d949c69ef12b2f6d1 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 17 Mar 2017 20:17:25 +0100 Subject: [PATCH 1/3] feat(cfg): write also enum choices when generating config.py files --- traitlets/config/configurable.py | 3 +++ traitlets/config/tests/test_configurable.py | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/traitlets/config/configurable.py b/traitlets/config/configurable.py index bb7ebad7..c5f25a1b 100644 --- a/traitlets/config/configurable.py +++ b/traitlets/config/configurable.py @@ -309,6 +309,9 @@ def c(s): for name, trait in sorted(cls.class_own_traits(config=True).items()): lines.append(c(trait.help)) + if 'Enum' in type(trait).__name__: + # include Enum choices + lines.append('# Choices: %r' % (trait.values,)) lines.append('#c.%s.%s = %s' % (cls.__name__, name, trait.default_value_repr())) lines.append('') return '\n'.join(lines) diff --git a/traitlets/config/tests/test_configurable.py b/traitlets/config/tests/test_configurable.py index b3ccae1b..4757fc6b 100644 --- a/traitlets/config/tests/test_configurable.py +++ b/traitlets/config/tests/test_configurable.py @@ -17,7 +17,7 @@ ) from traitlets.traitlets import ( - Integer, Float, Unicode, List, Dict, Set, + Integer, Float, Unicode, List, Dict, Set, Enum, _deprecations_shown, ) @@ -185,6 +185,14 @@ def test_help_inst(self): inst = MyConfigurable(a=5, b=4) self.assertEqual(MyConfigurable.class_get_help(inst), mc_help_inst) + def test_gen_enums_help_msg(self): + class MyConf(Configurable): + an_enum = Enum('Choice1 choice2'.split(), + help="Many choices.").tag(config=True) + enum_values_str = "Choices: ['Choice1', 'choice2']" + self.assertIn(enum_values_str, MyConf.class_get_help()) + self.assertIn(enum_values_str, MyConf.class_config_section()) + class TestSingletonConfigurable(TestCase): From 169e849263f909141a7aea14e1a56a4d618f4360 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Wed, 22 Mar 2017 13:59:58 +0100 Subject: [PATCH 2/3] doc(cfg): print enum-choices above default-value on help msg --- traitlets/config/configurable.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/traitlets/config/configurable.py b/traitlets/config/configurable.py index c5f25a1b..ac7ef497 100644 --- a/traitlets/config/configurable.py +++ b/traitlets/config/configurable.py @@ -260,6 +260,10 @@ def class_get_trait_help(cls, trait, inst=None, helptext=None): helptext = '\n'.join(wrap_paragraphs(helptext, 76)) lines.append(indent(helptext, 4)) + if 'Enum' in trait.__class__.__name__: + # include Enum choices + lines.append(indent('Choices: %r' % (trait.values,))) + if inst is not None: lines.append(indent('Current: %r' % getattr(inst, trait.name), 4)) else: @@ -271,9 +275,6 @@ def class_get_trait_help(cls, trait, inst=None, helptext=None): if len(dvr) > 64: dvr = dvr[:61]+'...' lines.append(indent('Default: %s' % dvr, 4)) - if 'Enum' in trait.__class__.__name__: - # include Enum choices - lines.append(indent('Choices: %r' % (trait.values,))) return '\n'.join(lines) From 0b9006be8aafd7a680bed3d2a51519583e301d7a Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Wed, 22 Mar 2017 14:14:21 +0100 Subject: [PATCH 3/3] doc(cfg, #381): print default-value on generated config-file comments --- traitlets/config/configurable.py | 6 ++- traitlets/config/tests/test_configurable.py | 42 ++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/traitlets/config/configurable.py b/traitlets/config/configurable.py index ac7ef497..d48c1e8b 100644 --- a/traitlets/config/configurable.py +++ b/traitlets/config/configurable.py @@ -310,10 +310,14 @@ def c(s): for name, trait in sorted(cls.class_own_traits(config=True).items()): lines.append(c(trait.help)) + if 'Enum' in type(trait).__name__: # include Enum choices lines.append('# Choices: %r' % (trait.values,)) - lines.append('#c.%s.%s = %s' % (cls.__name__, name, trait.default_value_repr())) + + dvr = trait.default_value_repr() + lines.append(indent('# Default: %s' % dvr, 4)) + lines.append('#c.%s.%s = %s' % (cls.__name__, name, dvr)) lines.append('') return '\n'.join(lines) diff --git a/traitlets/config/tests/test_configurable.py b/traitlets/config/tests/test_configurable.py index 4757fc6b..2fbe21de 100644 --- a/traitlets/config/tests/test_configurable.py +++ b/traitlets/config/tests/test_configurable.py @@ -185,13 +185,45 @@ def test_help_inst(self): inst = MyConfigurable(a=5, b=4) self.assertEqual(MyConfigurable.class_get_help(inst), mc_help_inst) - def test_gen_enums_help_msg(self): + def test_generated_config_enum_comments(self): class MyConf(Configurable): an_enum = Enum('Choice1 choice2'.split(), - help="Many choices.").tag(config=True) - enum_values_str = "Choices: ['Choice1', 'choice2']" - self.assertIn(enum_values_str, MyConf.class_get_help()) - self.assertIn(enum_values_str, MyConf.class_config_section()) + help="Many choices.").tag(config=True) + + help_str = "Many choices." + enum_choices_str = "Choices: ['Choice1', 'choice2']" + + self.assertIn(help_str, MyConf.class_get_help()) + self.assertIn(enum_choices_str, MyConf.class_get_help()) + + self.assertIn(help_str, MyConf.class_config_section()) + self.assertIn(enum_choices_str, MyConf.class_config_section()) + ## Check order of Help-msg <--> Choices sections + self.assertGreater(MyConf.class_config_section().index(enum_choices_str), + MyConf.class_config_section().index(help_str)) + + class MyConf2(Configurable): + an_enum = Enum('Choice1 choice2'.split(), + default_value='choice2', + help="Many choices.").tag(config=True) + + defaults_str = "Default: 'choice2'" + + self.assertIn(help_str, MyConf2.class_get_help()) + self.assertIn(enum_choices_str, MyConf2.class_get_help()) + self.assertIn(defaults_str, MyConf2.class_get_help()) + ## Check order of Default <--> Choices sections + self.assertGreater(MyConf2.class_get_help().index(defaults_str), + MyConf2.class_get_help().index(enum_choices_str)) + + self.assertIn(help_str, MyConf2.class_config_section()) + self.assertIn(enum_choices_str, MyConf2.class_config_section()) + self.assertIn(defaults_str, MyConf2.class_config_section()) + ## Check order of Default <--> Choices sections + self.assertGreater(MyConf2.class_config_section().index(defaults_str), + MyConf2.class_config_section().index(enum_choices_str)) + + class TestSingletonConfigurable(TestCase):