Skip to content

Commit

Permalink
fixes for Python<=3.8. Be neater about field naming
Browse files Browse the repository at this point in the history
  • Loading branch information
o-smirnov committed Feb 14, 2024
1 parent 0595f1d commit ecdc0b2
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions scabha/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +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)
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 keyword.issoftkeyword(fldname) or fldname in field2name:
fldname += f"_{num}"
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
Expand Down

0 comments on commit ecdc0b2

Please sign in to comment.