Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cfg): write also enum choices when generating config.py files #381

Merged
merged 3 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions traitlets/config/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -309,7 +310,14 @@ def c(s):

for name, trait in sorted(cls.class_own_traits(config=True).items()):
lines.append(c(trait.help))
lines.append('#c.%s.%s = %s' % (cls.__name__, name, trait.default_value_repr()))

if 'Enum' in type(trait).__name__:
# include Enum choices
lines.append('# Choices: %r' % (trait.values,))

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)

Expand Down
42 changes: 41 additions & 1 deletion traitlets/config/tests/test_configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)

from traitlets.traitlets import (
Integer, Float, Unicode, List, Dict, Set,
Integer, Float, Unicode, List, Dict, Set, Enum,
_deprecations_shown,
)

Expand Down Expand Up @@ -185,6 +185,46 @@ def test_help_inst(self):
inst = MyConfigurable(a=5, b=4)
self.assertEqual(MyConfigurable.class_get_help(inst), mc_help_inst)

def test_generated_config_enum_comments(self):
class MyConf(Configurable):
an_enum = Enum('Choice1 choice2'.split(),
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):

Expand Down