make option redeclaration silent if signature is the same

This commit is contained in:
Maximilian Hils 2017-07-30 02:14:44 +02:00
parent ecc01b0f02
commit 6a41122990
2 changed files with 20 additions and 1 deletions

View File

@ -86,7 +86,18 @@ class Loader:
choices: typing.Optional[typing.Sequence[str]] = None
) -> None:
if name in self.master.options:
ctx.log.warn("Over-riding existing option %s" % name)
existing = self.master.options._options[name]
same_signature = (
existing.name == name and
existing.typespec == typespec and
existing.default == default and
existing.help == help and
existing.choices == choices
)
if same_signature:
return
else:
ctx.log.warn("Over-riding existing option %s" % name)
self.master.options.add_option(
name,
typespec,

View File

@ -91,7 +91,15 @@ def test_loader():
with taddons.context() as tctx:
l = addonmanager.Loader(tctx.master)
l.add_option("custom_option", bool, False, "help")
assert "custom_option" in l.master.options
# calling this again with the same signature is a no-op.
l.add_option("custom_option", bool, False, "help")
assert not tctx.master.has_log("Over-riding existing option")
# a different signature should emit a warning though.
l.add_option("custom_option", bool, True, "help")
assert tctx.master.has_log("Over-riding existing option")
def cmd(a: str) -> str:
return "foo"