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

copy self.config in update_config #249

Merged
merged 2 commits into from
Jun 27, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions traitlets/config/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ def _config_changed(self, change):

def update_config(self, config):
"""Update config and load the new values"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given these specific semantics (deepcopy first before update), I think it would be a good idea to extend a little bit the docstring with a note that indicates that's what happens, so it becomes clearly official that such semantics are the intended ones.

# traitlets prior to 4.2 created a copy of self.config in order to trigger change events.
# Some projects (IPython < 5) relied upon one side effect of this,
# that self.config prior to update_config was not modified in-place.
# For backward-compatibility, we must ensure that self.config
# is a new object and not modified in-place,
# but config consumers should not rely on this behavior.
self.config = deepcopy(self.config)
# load config
self._load_config(config)
# merge it into self.config
Expand Down
26 changes: 26 additions & 0 deletions traitlets/config/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ def test_config_propagation(self):
self.assertEqual(app.foo.i, 10)
self.assertEqual(app.foo.j, 10)
self.assertEqual(app.bar.enabled, False)

def test_ipython_cli_priority(self):
name = 'config.py'
class TestApp(Application):
value = Unicode().tag(config=True)
aliases = {'v': 'TestApp.value'}
app = TestApp()
with TemporaryDirectory() as td:
config_file = pjoin(td, name)
with open(config_file, 'w') as f:
f.write("c.TestApp.value = 'config file'")
# follow IPython's config-loading sequence to ensure CLI priority is preserved
app.parse_command_line(['--v=cli'])
# this is where IPython makes a mistake:
# it assumes app.config will not be modified,
# and storing a reference is storing a copy
cli_config = app.config
assert 'value' in app.config.TestApp
assert app.config.TestApp.value == 'cli'
app.load_config_file(name, path=[td])
assert app.config.TestApp.value == 'config file'
# enforce cl-opts override config file opts:
# this is where IPython makes a mistake: it assumes
# that cl_config is a different object, but it isn't.
app.update_config(cli_config)
assert app.config.TestApp.value == 'cli'

def test_flags(self):
app = MyApp()
Expand Down