From 7ad8b8522b94c02d41fc0e8d7dff95f4eda89e80 Mon Sep 17 00:00:00 2001 From: Sebastian Goscik Date: Wed, 3 Feb 2016 14:59:49 +0000 Subject: [PATCH] AttributeCollection: No longer allows duplicate overriding attributes Previously if parameters with the same names and override set to True were added to an extension at the same level one would silently override the other. This is no longer the case and an error will be show instead. Also added tests to check that this is handeled correctly --- wlauto/core/extension.py | 6 +++++- wlauto/tests/test_extension.py | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/wlauto/core/extension.py b/wlauto/core/extension.py index 3aed90dc..348a5b9f 100644 --- a/wlauto/core/extension.py +++ b/wlauto/core/extension.py @@ -82,7 +82,12 @@ class AttributeCollection(object): return p def __iadd__(self, other): + other = [self._to_attrcls(p) for p in other] + names = [] for p in other: + if p.name in names: + raise ValueError("Duplicate '{}' {}".format(p.name, p.__class__.__name__.split('.')[-1])) + names.append(p.name) self.add(p) return self @@ -687,4 +692,3 @@ class Module(Extension): def initialize(self, context): pass - diff --git a/wlauto/tests/test_extension.py b/wlauto/tests/test_extension.py index 77e244f6..dee00cc4 100644 --- a/wlauto/tests/test_extension.py +++ b/wlauto/tests/test_extension.py @@ -320,6 +320,13 @@ class ParametersTest(TestCase): myext = _instantiate(MyOtherExtension, mandatory=1, optional='invalid') myext.validate() + @raises(ValueError) + def test_duplicate_param_override(self): + class DuplicateParamExtension(MyBaseExtension): # pylint: disable=W0612 + parameters = [ + Parameter('food', override=True, default='cheese'), + Parameter('food', override=True, default='cheese'), + ] class ModuleTest(TestCase): @@ -340,4 +347,3 @@ class ModuleTest(TestCase): def _instantiate(cls, *args, **kwargs): # Needed to get around Extension's __init__ checks return cls(*args, **kwargs) -