diff --git a/scabha/validate.py b/scabha/validate.py index e195e2c9..16f4a043 100644 --- a/scabha/validate.py +++ b/scabha/validate.py @@ -3,7 +3,7 @@ import os.path import yaml import re -import typing +import keyword from typing import * from collections import OrderedDict @@ -176,9 +176,15 @@ def validate_parameters(params: Dict[str, Any], schemas: Dict[str, Any], value = inputs.get(name, UNSET) if value is not UNSET: # sanitize name: dataclass won't take hyphens or periods - fldname = re.sub("\W", "_", name) - while fldname in field2name: - fldname += "_" + fldname = orig_fldname = re.sub("\\W", "_", name) + # avoid Python keywords and clashes with other field names by adding _x as needed + num = 0 + while keyword.iskeyword(fldname) or \ + (hasattr(keyword, 'issoftkeyword') and keyword.issoftkeyword(fldname)) or \ + fldname in field2name: + fldname += f"{orig_fldname}_{num}" + num += 1 + # add to mapping field2name[fldname] = name name2field[name] = fldname diff --git a/tests/stimela_tests/test_recipe.yml b/tests/stimela_tests/test_recipe.yml index e58a4072..67e2c58a 100644 --- a/tests/stimela_tests/test_recipe.yml +++ b/tests/stimela_tests/test_recipe.yml @@ -102,6 +102,12 @@ recipe: elemlist-xyz: dtype: Union[str, List[str]] element_choices: [x,y,z] + elemlist_xyz: + default: "deliberate use of _ vs - to check validate() avoiding this situation" + continue: + default: "deliberate use of Python keyword to make sure validate() doesn't barf" + with: + default: "deliberate use of Python keyword to make sure validate() doesn't barf" assign: