Skip to content

Commit

Permalink
Merge pull request #232 from caracal-pipeline/issue-136
Browse files Browse the repository at this point in the history
fixes #136
  • Loading branch information
o-smirnov authored Feb 14, 2024
2 parents cc0d0b4 + ecdc0b2 commit 3392a1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions scabha/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os.path
import yaml
import re
import typing
import keyword
from typing import *
from collections import OrderedDict

Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions tests/stimela_tests/test_recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3392a1f

Please sign in to comment.