Skip to content

Commit

Permalink
Adding an option to ConfigBase.__call__ to preserve implicit values
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Dec 17, 2018
1 parent ea05379 commit d04dfda
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pyutilib/misc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def __setstate__(self, state):

def __call__(self, value=NoArgument, default=NoArgument, domain=NoArgument,
description=NoArgument, doc=NoArgument, visibility=NoArgument,
implicit=NoArgument, implicit_domain=NoArgument):
implicit=NoArgument, implicit_domain=NoArgument,
preserve_implicit=False):
# We will pass through overriding arguments to the constructor.
# This way if the constructor does special processing of any of
# the arguments (like implicit_domain), we don't have to repeat
Expand Down Expand Up @@ -223,9 +224,9 @@ def __call__(self, value=NoArgument, default=NoArgument, domain=NoArgument,
ans = self.__class__(**kwds)
if isinstance(self, ConfigBlock):
for k in self._decl_order:
if k in self._declared:
if k in self._declared or preserve_implicit:
v = self._data[k]
ans._data[k] = _tmp = v()
ans._data[k] = _tmp = v(preserve_implicit=preserve_implicit)
ans._decl_order.append(k)
ans._declared.add(k)
_tmp._parent = ans
Expand Down
20 changes: 19 additions & 1 deletion pyutilib/misc/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,10 +1513,14 @@ def test_set_value(self):
def test_call_options(self):
config = ConfigBlock(description="base description",
doc="base doc",
visibility=1)
visibility=1,
implicit=True)
config.declare("a", ConfigValue(domain=int, doc="a doc", default=1))
config.declare("b", config.get("a")(2))
config.declare("c", config.get("a")(domain=float, doc="c doc"))
config.d = 0
config.e = ConfigBlock(implicit=True)
config.e.a = 0

reference_template = """# base description
"""
Expand All @@ -1525,9 +1529,23 @@ def test_call_options(self):
a: 1
b: 2
c: 1.0
d: 0
e:
a: 0
"""
self._validateTemplate(config, reference_template, visibility=1)

# Preserving implicit values should leave the copy the same as
# the original
implicit_copy = config(preserve_implicit=True)
self._validateTemplate(config, reference_template, visibility=1)

# Simple copies strip out the implicitly-declared values
reference_template = """# base description
a: 1
b: 2
c: 1.0
"""
simple_copy = config()
self._validateTemplate(simple_copy, reference_template, visibility=1)
self.assertEqual(simple_copy._doc, "base doc")
Expand Down

0 comments on commit d04dfda

Please sign in to comment.