Fix calling Task.get_parameters_as_dict(cast=True) raises a YAML parser error

This commit is contained in:
allegroai 2023-10-24 18:40:40 +03:00
parent f964783046
commit 0caa787d7d

View File

@ -1,6 +1,7 @@
import itertools import itertools
import json import json
from copy import copy from copy import copy
from logging import getLogger
import six import six
import yaml import yaml
@ -132,16 +133,21 @@ def cast_basic_type(value, type_str):
parts = type_str.split('/') parts = type_str.split('/')
# nested = len(parts) > 1 # nested = len(parts) > 1
if parts[0] in ('list', 'tuple'): if parts[0] in ("list", "tuple", "dict"):
v = '[' + value.lstrip('[(').rstrip('])') + ']' # noinspection PyBroadException
v = yaml.load(v, Loader=yaml.SafeLoader)
return basic_types.get(parts[0])(v)
elif parts[0] in ('dict', ):
try: try:
return json.loads(value) # lists/tuple/dicts should be json loadable
return basic_types.get(parts[0])(json.loads(value))
except Exception: except Exception:
pass # noinspection PyBroadException
return value try:
# fallback to legacy basic type loading
v = '[' + value.lstrip('[(').rstrip('])') + ']'
v = yaml.load(v, Loader=yaml.SafeLoader)
return basic_types.get(parts[0])(v)
except Exception:
getLogger().warning("Could not cast `{}` to basic type. Returning it as `str`".format(value))
return value
t = basic_types.get(str(type_str).lower().strip(), False) t = basic_types.get(str(type_str).lower().strip(), False)
if t is not False: if t is not False: