diff --git a/src/inmanta/ast/constraint/expression.py b/src/inmanta/ast/constraint/expression.py index 2232ef8a62..acdba3ad5e 100644 --- a/src/inmanta/ast/constraint/expression.py +++ b/src/inmanta/ast/constraint/expression.py @@ -437,7 +437,7 @@ def _is_final(self, result): class In(BinaryOperator): """ - The in operator for iterable types + The in operator for iterable types and dicts """ __op = "in" @@ -448,11 +448,13 @@ def _bin_op(self, arg1, arg2): """ @see Operator#_op """ - if not (isinstance(arg2, list) or (hasattr(arg2, "type") and arg2.type() == list)): - raise Exception("Operand two of 'in' can only be a list (%s)" % arg2[0]) - - for arg in arg2: - if arg == arg1: - return True + if isinstance(arg2, dict): + return arg1 in arg2 + elif isinstance(arg2, list): + for arg in arg2: + if arg == arg1: + return True + else: + raise Exception("Operand two of 'in' can only be a list or dict (%s)" % arg2[0]) return False diff --git a/tests/test_compilation.py b/tests/test_compilation.py index 20249e4ec5..d504c4b7f9 100644 --- a/tests/test_compilation.py +++ b/tests/test_compilation.py @@ -2272,3 +2272,37 @@ def test_lnr_on_double_is_defined(snippetcompiler): a.one = a """) compiler.do_compile() + + +def test_673_in_dict(snippetcompiler): + snippetcompiler.setup_for_snippet(""" +entity Test: + dict attributes +end + +implementation test for Test: + +end + +implement Test using test when "foo" in self.attributes + +Test(attributes={"foo": 42}) +""") + compiler.do_compile() + + +def test_673_in_list(snippetcompiler): + snippetcompiler.setup_for_snippet(""" +entity Test: + string[] attributes +end + +implementation test for Test: + +end + +implement Test using test when "foo" in self.attributes + +Test(attributes=["blah", "foo"]) +""") + compiler.do_compile()