Skip to content

Commit 74cfe20

Browse files
authored
Only allow setting .rule to a string representation of a rule (#475)
1 parent 024a561 commit 74cfe20

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

khiops/core/dictionary.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ def __init__(self, json_data=None):
11871187
self.structure_type = ""
11881188

11891189
# Derivation rule
1190-
self.rule = ""
1190+
self._rule = ""
11911191

11921192
# Metadata
11931193
self.meta_data = MetaData()
@@ -1223,7 +1223,7 @@ def __init__(self, json_data=None):
12231223
self.structure_type = json_data.get("structureType")
12241224

12251225
# Initialize derivation rule
1226-
self.rule = json_data.get("rule", "")
1226+
self._rule = json_data.get("rule", "")
12271227

12281228
# Initialize metadata
12291229
json_meta_data = json_data.get("metaData")
@@ -1249,6 +1249,16 @@ def name(self, value):
12491249
_check_name(value)
12501250
self._name = value
12511251

1252+
@property
1253+
def rule(self):
1254+
return self._rule
1255+
1256+
@rule.setter
1257+
def rule(self, value):
1258+
if not is_string_like(value):
1259+
raise TypeError(type_error_message("rule", value, "string-like"))
1260+
self._rule = value
1261+
12521262
def copy(self):
12531263
"""Copies this variable instance
12541264
@@ -1479,7 +1489,7 @@ def __init__(self, json_data=None):
14791489
self.internal_comments = json_data.get("internalComments", [])
14801490

14811491
# Initialize derivation rule
1482-
self.rule = json_data.get("rule", "")
1492+
self._rule = json_data.get("rule", "")
14831493

14841494
# Initialize metadata if available
14851495
json_meta_data = json_data.get("metaData")
@@ -1504,6 +1514,16 @@ def __str__(self):
15041514
self.write(writer)
15051515
return str(stream.getvalue(), encoding="utf8", errors="replace")
15061516

1517+
@property
1518+
def rule(self):
1519+
return self._rule
1520+
1521+
@rule.setter
1522+
def rule(self, value):
1523+
if not is_string_like(value):
1524+
raise TypeError(type_error_message("rule", value, "string-like"))
1525+
self._rule = value
1526+
15071527
def add_variable(self, variable):
15081528
"""Adds a variable to this block
15091529

tests/test_core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,9 @@ def test_dictionary_accessors(self):
19161916
repr(dictionary.get_variable("fresh_one").get_rule()),
19171917
"Variable rule must be set correctly",
19181918
)
1919+
variable_rule = kh.Rule(verbatim="Ceil(Product(3, Random()))")
1920+
with self.assertRaises(TypeError):
1921+
dictionary.get_variable("fresh_one").rule = variable_rule
19191922

19201923
# Test Dictionary variable block accessors
19211924
# Create a simple block
@@ -1959,7 +1962,9 @@ def test_dictionary_accessors(self):
19591962

19601963
# Set the block as non-native add, and remove it
19611964
dictionary_copy.add_variable_block(block)
1962-
block_rule = kh.Rule("SomeBlockCreatingRule()")
1965+
block_rule = kh.Rule("SomeBlockCreatingRule")
1966+
with self.assertRaises(TypeError):
1967+
dictionary_copy.get_variable_block(block.name).rule = block_rule
19631968
dictionary_copy.get_variable_block(block.name).set_rule(block_rule)
19641969
self.assertEqual(block, dictionary_copy.get_variable_block(block.name))
19651970
removed_block = dictionary_copy.remove_variable_block(

0 commit comments

Comments
 (0)