text
stringlengths
0
828
# right now we're stuck with the default logger..
logr = default_logger
if to_type is None or is_any_type(to_type):
# explicitly handle the 'None' (joker) or 'any' type
return True
elif is_collection(to_type, strict=True):
# if the destination type is 'strictly a collection' (not a subclass of a collection) we know that we can't
# handle it here, the constructor is not pep484-typed
return False
else:
# (1) Try the type itself
try:
# can we find enough pep-484 information in the constructor to be able to understand what is required ?
get_constructor_attributes_types(to_type)
return True
except TypeInformationRequiredError as main_e:
# # failed: we cant guess the required types of constructor arguments
# if strict_mode:
# # Warning and return NO
# if should_display_warnings_for(to_type):
# logr.warn('Object constructor signature for type {} does not allow parsyfiles to '
# 'automatically create instances from dict content. Caught {}: {}'
# ''.format(get_pretty_type_str(to_type), type(main_e).__name__, main_e))
# return False
#
# # non-strict mode: (2) Check if any subclasses exist
# subclasses = get_all_subclasses(to_type)
# if len(subclasses) > GLOBAL_CONFIG.dict_to_object_subclass_limit:
# logr.warn('WARNING: Type {} has {} subclasses, only {} will be tried by parsyfiles when attempting to '
# 'create it from a subclass. You can raise this limit by setting the appropriate option with '
# '`parsyfiles_global_config()`'
# ''.format(to_type, len(subclasses), GLOBAL_CONFIG.dict_to_object_subclass_limit))
#
# # Then for each subclass also try (with a configurable limit in nb of subclasses)
# for subclass in subclasses[0:GLOBAL_CONFIG.dict_to_object_subclass_limit]:
# try:
# get_constructor_attributes_types(subclass)
# # OK, but issue warning for the root type still
# if should_display_warnings_for(to_type):
# logr.warn('WARNING: Object constructor signature for type {} does not allow parsyfiles to '
# 'automatically create instances from dict content, but it can for at least one of '
# 'its subclasses ({}) so it might be ok for you. Caught {}: {}'
# ''.format(get_pretty_type_str(to_type), get_pretty_type_str(subclass),
# type(main_e).__name__, main_e))
# return True
# except TypeInformationRequiredError as e:
# # failed: we cant guess the required types of constructor arguments
# if should_display_warnings_for(to_type):
# logr.warn('WARNING: Object constructor signature for type {} does not allow parsyfiles to '
# 'automatically create instances from dict content. Caught {}: {}'
# ''.format(subclass, type(e).__name__, e))
#
# # Nothing succeeded
if should_display_warnings_for(to_type):
logr.warn('WARNING: Object constructor signature for type {} does not allow parsyfiles to '
'automatically create instances from dict content. Caught {}: {}'
''.format(get_pretty_type_str(to_type), type(main_e).__name__, main_e))
return False"
352,"def dict_to_object(desired_type: Type[T], contents_dict: Dict[str, Any], logger: Logger,
options: Dict[str, Dict[str, Any]], conversion_finder: ConversionFinder = None,
is_dict_of_dicts: bool = False) -> T:
""""""
Utility method to create an object from a dictionary of constructor arguments. Constructor arguments that dont have
the correct type are intelligently converted if possible
:param desired_type:
:param contents_dict:
:param logger:
:param options:
:param conversion_finder:
:param is_dict_of_dicts:
:return:
""""""
check_var(desired_type, var_types=type, var_name='obj_type')
check_var(contents_dict, var_types=dict, var_name='contents_dict')
if is_collection(desired_type, strict=True):
# if the destination type is 'strictly a collection' (not a subclass of a collection) we know that we can't
# handle it here, the constructor is not pep484-typed
raise TypeError('Desired object type \'' + get_pretty_type_str(desired_type) + '\' is a collection, '
'so it cannot be created using this generic object creator')
else:
# Try the type itself
# try:
return _dict_to_object(desired_type, contents_dict, logger=logger, options=options,
conversion_finder=conversion_finder, is_dict_of_dicts=is_dict_of_dicts)"
353,"def _dict_to_object(desired_type: Type[T], contents_dict: Dict[str, Any], logger: Logger,
options: Dict[str, Dict[str, Any]], conversion_finder: ConversionFinder = None,
is_dict_of_dicts: bool = False) -> T:
""""""
Utility method to create an object from a dictionary of constructor arguments. Constructor arguments that dont have
the correct type are intelligently converted if possible
:param desired_type:
:param contents_dict: