Exemplo n.º 1
0
 def init_component(self, path):
     """
 Args:
   path: path to uninitialized object
 Returns:
   initialized object; this method is cached, so multiple requests for the same path will return the exact same object
 """
     obj = tree_tools.get_descendant(self.deserialized_yaml, path)
     if not isinstance(obj, Serializable):
         return obj
     init_params = OrderedDict(
         tree_tools.name_children(obj, include_reserved=False))
     serialize_params = OrderedDict(init_params)
     init_args = tree_tools.get_init_args_defaults(obj)
     if "yaml_path" in init_args: init_params["yaml_path"] = path
     self.check_init_param_types(obj, init_params)
     try:
         initialized_obj = obj.__class__(**init_params)
         logger.debug(
             f"initialized {path}: {obj.__class__.__name__}@{id(obj)}({dict(init_params)})"[:
                                                                                            1000]
         )
     except TypeError as e:
         raise ComponentInitError(
             f"An error occurred trying to invoke {type(obj).__name__}.__init__()\n"
             f" The following arguments were passed: {init_params}\n"
             f" The following arguments were expected: {init_args.keys()}\n"
             f" Error message: {e}")
     serialize_params.update(
         getattr(initialized_obj, "serialize_params", {}))
     initialized_obj.serialize_params = serialize_params
     return initialized_obj
Exemplo n.º 2
0
 def format_strings(self, exp_values, format_dict):
     """
 - replaces strings containing {EXP} and other supported args
 - also checks if there are default arguments for which no arguments are set and instantiates them with replaced {EXP} if applicable
 """
     for path, node in tree_tools.traverse_tree(exp_values):
         if isinstance(node, str):
             try:
                 formatted = node.format(**format_dict)
             except (
                     ValueError, KeyError
             ):  # will occur e.g. if a vocab entry contains a curly bracket
                 formatted = node
             if node != formatted:
                 tree_tools.set_descendant(exp_values, path,
                                           FormatString(formatted, node))
         elif isinstance(node, Serializable):
             init_args_defaults = tree_tools.get_init_args_defaults(node)
             for expected_arg in init_args_defaults:
                 if not expected_arg in [
                         x[0] for x in tree_tools.name_children(
                             node, include_reserved=False)
                 ]:
                     arg_default = init_args_defaults[expected_arg].default
                     if isinstance(arg_default, str):
                         try:
                             formatted = arg_default.format(**format_dict)
                         except (
                                 ValueError, KeyError
                         ):  # will occur e.g. if a vocab entry contains a curly bracket
                             formatted = arg_default
                         if arg_default != formatted:
                             setattr(node, expected_arg,
                                     FormatString(formatted, arg_default))
Exemplo n.º 3
0
 def resolve_ref_default_args(self, root):
     for _, node in tree_tools.traverse_tree(root):
         if isinstance(node, Serializable):
             init_args_defaults = tree_tools.get_init_args_defaults(node)
             for expected_arg in init_args_defaults:
                 if not expected_arg in [
                         x[0] for x in tree_tools.name_children(
                             node, include_reserved=False)
                 ]:
                     arg_default = init_args_defaults[expected_arg].default
                     if isinstance(arg_default, tree_tools.Ref):
                         setattr(node, expected_arg, arg_default)
Exemplo n.º 4
0
 def check_init_param_types(self, obj, init_params):
     for init_param_name in init_params:
         param_sig = tree_tools.get_init_args_defaults(obj)
         if init_param_name in param_sig:
             annotated_type = param_sig[init_param_name].annotation
             if annotated_type != inspect.Parameter.empty:
                 try:
                     if not isinstance(init_params[init_param_name],
                                       annotated_type):
                         raise ValueError(
                             f"type check failed for {init_param_name} argument of {obj}: expected {annotated_type}, received {init_params[init_param_name]} of type {type(init_params[init_param_name])}"
                         )
                 except TypeError:
                     pass  # isinstance does not work with types from Python's "typing" module, let's skip test
Exemplo n.º 5
0
 def share_init_params_top_down(self, root):
     abs_shared_param_sets = []
     for path, node in tree_tools.traverse_tree(root):
         if isinstance(node, Serializable):
             for shared_param_set in node.shared_params():
                 abs_shared_param_set = set(
                     p.get_absolute(path) for p in shared_param_set)
                 added = False
                 for prev_set in abs_shared_param_sets:
                     if prev_set & abs_shared_param_set:
                         prev_set |= abs_shared_param_set
                         added = True
                         break
                 if not added:
                     abs_shared_param_sets.append(abs_shared_param_set)
     for shared_param_set in abs_shared_param_sets:
         shared_val_choices = set()
         for shared_param_path in shared_param_set:
             try:
                 new_shared_val = tree_tools.get_descendant(
                     root, shared_param_path)
             except tree_tools.PathError:
                 continue
             for _, child_of_shared_param in tree_tools.traverse_tree(
                     new_shared_val, include_root=False):
                 if isinstance(child_of_shared_param, Serializable):
                     raise ValueError(
                         f"{path} shared params {shared_param_set} contains Serializable sub-object {child_of_shared_param} which is not permitted"
                     )
             shared_val_choices.add(new_shared_val)
         if len(shared_val_choices) > 1:
             logger.warning(
                 f"inconsistent shared params at {path} for {shared_param_set}: {shared_val_choices}; Ignoring these shared parameters."
             )
         elif len(shared_val_choices) == 1:
             for shared_param_path in shared_param_set:
                 if shared_param_path[
                         -1] in tree_tools.get_init_args_defaults(
                             tree_tools.get_descendant(
                                 root, shared_param_path.parent())):
                     tree_tools.set_descendant(root, shared_param_path,
                                               list(shared_val_choices)[0])
Exemplo n.º 6
0
 def resolve_bare_default_args(self, root):
     for path, node in tree_tools.traverse_tree(root):
         if isinstance(node, Serializable):
             init_args_defaults = tree_tools.get_init_args_defaults(node)
             for expected_arg in init_args_defaults:
                 if not expected_arg in [
                         x[0] for x in tree_tools.name_children(
                             node, include_reserved=False)
                 ]:
                     arg_default = init_args_defaults[expected_arg].default
                     if isinstance(arg_default,
                                   Serializable) and not isinstance(
                                       arg_default, tree_tools.Ref):
                         if not getattr(arg_default, "_is_bare", False):
                             raise ValueError(
                                 f"only Serializables created via bare(SerializableSubtype) are permitted as default arguments; found a fully initialized Serializable: {arg_default} at {path}"
                             )
                         self.resolve_bare_default_args(
                             arg_default)  # apply recursively
                         setattr(node, expected_arg, arg_default)
Exemplo n.º 7
0
 def create_referenced_default_args(self, root):
     for _, node in tree_tools.traverse_tree(root):
         if isinstance(node, tree_tools.Ref):
             referenced_path = node.get_path()
             if not referenced_path:
                 continue  # skip named paths
             if isinstance(referenced_path, str):
                 referenced_path = tree_tools.Path(referenced_path)
             give_up = False
             for ancestor in sorted(referenced_path.ancestors(),
                                    key=lambda x: len(x)):
                 try:
                     tree_tools.get_descendant(root, ancestor)
                 except tree_tools.PathError:
                     ancestor_parent = tree_tools.get_descendant(
                         root, ancestor.parent())
                     if isinstance(ancestor_parent, Serializable):
                         init_args_defaults = tree_tools.get_init_args_defaults(
                             ancestor_parent)
                         if ancestor[-1] in init_args_defaults:
                             referenced_arg_default = init_args_defaults[
                                 ancestor[-1]].default
                         else:
                             referenced_arg_default = inspect.Parameter.empty
                         if referenced_arg_default == inspect.Parameter.empty:
                             if node.is_required():
                                 raise ValueError(
                                     f"Reference '{node}' is required but does not exist and has no default arguments"
                                 )
                         else:
                             tree_tools.set_descendant(
                                 root, ancestor, referenced_arg_default)
                     else:
                         if node.is_required():
                             raise ValueError(
                                 f"Reference '{node}' is required but does not exist"
                             )
                         give_up = True
                 if give_up: break