Skip to content
Snippets Groups Projects
Commit 7f794c5d authored by Marco Clemencic's avatar Marco Clemencic
Browse files

GaudiConfig2: strict type checking for list properties

See merge request !1518
parents 93443e00 77a94cfa
No related branches found
No related tags found
1 merge request!1518GaudiConfig2: strict type checking for list properties
Pipeline #6541567 passed
......@@ -148,7 +148,7 @@ class FloatSemantics(PropertySemantics):
if not isinstance(value, Number):
raise TypeError(
"number expected, got {!r} in assignemnt to {}".format(value, self.name)
"number expected, got {!r} in assignment to {}".format(value, self.name)
)
return float(value)
......@@ -360,6 +360,12 @@ class SequenceSemantics(PropertySemantics):
self.value_semantics.name = "{} element".format(self._name)
def store(self, value):
if not isinstance(value, (list, _ListHelper, tuple)):
raise TypeError(
"list or tuple expected, got {!r} in assignment to {}".format(
value, self.name
)
)
new_value = _ListHelper(self.value_semantics)
new_value.extend(value)
return new_value
......@@ -494,6 +500,7 @@ class MappingSemantics(PropertySemantics):
self.value_semantics.name = "{} value".format(self._name)
def store(self, value):
# No explicit type checking as anything else than dict fails in update call
new_value = _DictHelper(self.key_semantics, self.value_semantics)
new_value.update(value)
return new_value
......
......@@ -95,6 +95,12 @@ def test_access():
assert d.get("q", "Q") == "Q"
def test_assignment_bad_type():
s = S.getSemanticsFor("std::map<std::string, std::string>")
with pytest.raises(AttributeError):
s.store([("a", 1), ("b", 2)])
def test_assignment_bad_value():
s = S.getSemanticsFor("std::map<std::string, std::string>")
with pytest.raises(TypeError):
......
......@@ -84,11 +84,26 @@ def test_opt_value():
assert s.opt_value(d) == ["a", "b", "c"]
def test_implicit_conversion():
s = S.getSemanticsFor("std::vector<std::string, alloc<string> >")
# For backwards-compatibility (i.e. for Property<DataObjIDColl>)
# we support assignment from tuple:
d = s.store(("a", "b", "c"))
assert d == list("abc")
def test_assignment_bad():
s = S.getSemanticsFor("std::vector<std::string, alloc<string> >")
with pytest.raises(TypeError):
s.store(["a", "b", 1])
with pytest.raises(TypeError):
s.store("ab")
with pytest.raises(TypeError):
s.store(set("ab"))
def test_in_alg():
p = AlgWithVectors()
......
#####################################################################################
# (c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations #
# (c) Copyright 1998-2023 CERN for the benefit of the LHCb and ATLAS collaborations #
# #
# This software is distributed under the terms of the Apache version 2 licence, #
# copied verbatim in the file "LICENSE". #
......@@ -50,7 +50,7 @@ def test_repr(with_global_instances):
assert t1.name == orig_name
assert t1.Bool == t.Bool
p = AlgWithVectors(VS="abc")
p = AlgWithVectors(VS=list("abc"))
q = eval(repr(p))
assert list(q.VS) == list("abc")
assert repr(q) == repr(p)
......@@ -88,7 +88,7 @@ def test_pickle(with_global_instances):
assert t1.name == orig_name
assert t1.Bool == t.Bool
p = AlgWithVectors(VS="abc")
p = AlgWithVectors(VS=list("abc"))
q = loads(dumps(p))
assert list(q.VS) == list("abc")
assert dumps(q) == dumps(p)
......@@ -107,7 +107,7 @@ def test_pickle(with_global_instances):
def test_full_serialization_repr(with_global_instances):
MyAlg("Alg1", AnIntProp=1)
SimpleOptsAlgTool("ToolA", parent=MyAlg("Alg2", AnIntProp=2))
AlgWithVectors("AV", VS="abc")
AlgWithVectors("AV", VS=list("abc"))
AlgWithMaps("AM", MSS={"a": "B"})
serial = repr(list(Configurable.instances.values()))
......@@ -122,7 +122,7 @@ def test_full_serialization_pickle(with_global_instances):
MyAlg("Alg1", AnIntProp=1)
SimpleOptsAlgTool("ToolA", parent=MyAlg("Alg2", AnIntProp=2))
AlgWithVectors("AV", VS="abc")
AlgWithVectors("AV", VS=list("abc"))
AlgWithMaps("AM", MSS={"a": "B"})
serial = dumps(list(Configurable.instances.values()))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment