Skip to content

fill_defaults

allennlp_hydra.config.fill_defaults

[SOURCE]


Module for adding arguments and their default values to a config.

fill_config_with_default_values#

def fill_config_with_default_values(
    base_class: Union[FromParams, Registrable],
    config: Dict
) -> Dict

Fill a config with the arguments and their default values from a base_class. When it encounters nested objects (i.e. non-builtin classes, for now limited to AllenNLP FromParams and Registrable) it will recurse and create the dictionary with defaults.

This does NOT change the mutable config nor does it override keys if they exist in the config.

NOTE: When AllenNLP expands upon their to_params api, this will rely on that, but for the time being it relies on the inspect module.

Parameters

  • base_class : Union[FromParams, Registrable]
    The base class that you want to use to fill the config with the arguments and their default values. These arguments are for the base_class.__init__ function.
  • config : Dict
    The configuration dict to fill with the defaults. It does NOT make any changes to the mutable data but instead uses deepcopy to copy it to a new object. Existing keys will not be overwritten.

Returns

  • cfg_with_defaults : Dict The filled config with defaults.

get_default_value_for_parameter#

def get_default_value_for_parameter(
    parameter: inspect.Parameter
) -> Any

Get the default value for a parameter.

If there is a type annotation, check if it is a subclass of Registrable. If that is the case, check if the default value is an instance of that class.

If it is, then recurse. Otherwise continue. Handles the case where the default value is an initialized registrable. We also find what name the class was registered as to add that as the type value.

Parameters

parameter: `inspect.Parameter`
    The parameter to get the default value of.

Returns

  • Any The default value of the parameter.

get_annotation_class#

def get_annotation_class(
    parameter: inspect.Parameter
) -> Union[type, None]

Get the class of the annotation.

Parameters

parameter: `inspect.Parameter`
    The parameter to get the class of.

Returns

  • Union[type, None] The type or None.

get_positional_arguments#

def get_positional_arguments(cls_type: FromParams) -> Iterable[str]

Get the init parameters from the underlying class of initialized_class