text
stringlengths
0
828
# nothing is required in terms of dict values: use the base method
return ConversionFinder.try_convert_value(conversion_finder, '', coll_to_convert, desired_type,
logger=logger, options=kwargs)
else:
# TODO resuse appropriate container type (not necessary a dict) according to type of coll_to_convert
# there is a specific type required for the dict values.
res = dict()
# convert if required
for key, val in coll_to_convert.items():
res[key] = ConversionFinder.try_convert_value(conversion_finder, key, val, item_typ, logger,
options=kwargs)
return res
elif issubclass(base_desired_type, Sequence): # or issubclass(base_desired_type, list):
# get the base collection type if provided
item_typ, _ = _extract_collection_base_type(desired_type, exception_if_none=False)
if item_typ is None:
# nothing is required in terms of dict values: use the base method
return ConversionFinder.try_convert_value(conversion_finder, '', coll_to_convert, desired_type,
logger=logger, options=kwargs)
else:
# TODO resuse appropriate container type (not necessary a list) according to type of coll_to_convert
# there is a specific type required for the list values.
res = list()
# special case where base_desired_type is a Tuple: in that case item_typ may be a tuple or else
if type(item_typ) != tuple:
# convert each item if required
for val in coll_to_convert:
res.append(ConversionFinder.try_convert_value(conversion_finder, '', val, item_typ, logger,
options=kwargs))
else:
if len(item_typ) == 1:
item_typ_tuple = item_typ * len(coll_to_convert)
elif len(item_typ) == len(coll_to_convert):
item_typ_tuple = item_typ
else:
raise ValueError('Collection to convert is of length {} which is not compliant with desired '
'type {}'.format(len(coll_to_convert), item_typ))
for val, item_t in zip(coll_to_convert, item_typ_tuple):
res.append(ConversionFinder.try_convert_value(conversion_finder, '', val, item_t, logger,
options=kwargs))
res = tuple(res)
return res
elif issubclass(base_desired_type, AbstractSet): # or issubclass(base_desired_type, set):
# get the base collection type if provided
item_typ, _ = _extract_collection_base_type(desired_type, exception_if_none=False)
if item_typ is None:
# nothing is required in terms of dict values: use the base method
return ConversionFinder.try_convert_value(conversion_finder, '', coll_to_convert, desired_type,
logger=logger, options=kwargs)
else:
# TODO resuse appropriate container type (not necessary a set) according to type of coll_to_convert
# there is a specific type required for the list values.
res = set()
# convert if required
for val in coll_to_convert:
res.add(ConversionFinder.try_convert_value(conversion_finder, '', val, item_typ, logger,
options=kwargs))
return res
else:
raise TypeError('Cannot convert collection values, expected type is not a supported collection '
'(dict, list, set, Mapping, Sequence, AbstractSet)! : ' + str(desired_type))"
510,"def _try_convert_value(conversion_finder, attr_name: str, attr_value: S, desired_attr_type: Type[T], logger: Logger,
options: Dict[str, Dict[str, Any]]) -> T:
""""""
Utility method to try to use provided conversion_finder to convert attr_value into desired_attr_type.
If no conversion is required, the conversion finder is not even used (it can be None)
:param conversion_finder:
:param attr_name:
:param attr_value:
:param desired_attr_type:
:param logger:
:param options:
:return:
""""""
# check if we need additional conversion
# (a) a collection with details about the internal item type
if is_typed_collection(desired_attr_type):
return ConversionFinder.convert_collection_values_according_to_pep(coll_to_convert=attr_value,
desired_type=desired_attr_type,
conversion_finder=conversion_finder,
logger=logger,
**options)
# --- typing types do not work with isinstance so there is a special check here
elif not robust_isinstance(attr_value, desired_attr_type):
if conversion_finder is not None:
return conversion_finder.find_and_convert(attr_name,
attr_value,
desired_attr_type,
logger, options)
else: