Skip to content

Commit

Permalink
fixes #136
Browse files Browse the repository at this point in the history
  • Loading branch information
o-smirnov committed Feb 14, 2024
1 parent 1cdf38e commit 0595f1d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 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,13 @@ 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 = 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 keyword.issoftkeyword(fldname) or fldname in field2name:
fldname += f"_{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 0595f1d

Please sign in to comment.