def expand(self, repex_vars, fields):
logger.debug('<STR_LIT>')<EOL>unexpanded_instances = set()<EOL>for k, v in repex_vars.items():<EOL><INDENT>repex_vars[k] = self._expand_var(v, repex_vars)<EOL>instances = self._get_instances(repex_vars[k])<EOL>unexpanded_instances.update(instances)<EOL><DEDENT>for key in fields.keys():<EOL><INDENT>field = fields[key]<EOL>if isinstance(field, str):<EOL><INDENT>fields[key] = self._expand_var(field, repex_vars)<EOL>instances = self._get_instances(fields[key])<EOL>unexpanded_instances.update(instances)<EOL><DEDENT>elif isinstance(field, dict):<EOL><INDENT>for k, v in field.items():<EOL><INDENT>fields[key][k] = self._expand_var(v, repex_vars)<EOL>instances = self._get_instances(fields[key][k])<EOL>unexpanded_instances.update(instances)<EOL><DEDENT><DEDENT>elif isinstance(field, list):<EOL><INDENT>for index, item in enumerate(field):<EOL><INDENT>fields[key][index] = self._expand_var(item, repex_vars)<EOL>instances = self._get_instances(fields[key][index])<EOL>unexpanded_instances.update(instances)<EOL><DEDENT><DEDENT><DEDENT>if unexpanded_instances:<EOL><INDENT>raise RepexError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(<EOL>list(unexpanded_instances)))<EOL><DEDENT>return fields<EOL>
r"""Receive a dict of variables and a dict of fields
        and iterates through them to expand a variable in an field, then
        returns the fields dict with its variables expanded.

        This will fail if not all variables expand (due to not providing
        all necessary ones).

        fields:

        type: VERSION
        path: resources
        excluded:
            - excluded_file.file
        base_directory: '{{ .base_dir }}'
        match: '"version": "\d+\.\d+(\.\d+)?(-\w\d+)?'
        replace: \d+\.\d+(\.\d+)?(-\w\d+)?
        with: "{{ .version }}"
        must_include:
            - {{ .my_var }}/{{ .another_var }}
            - {{ .my_other_var }}
            - version
        validator:
            type: per_file
            path: {{ .my_validator_path }}
            function: validate

        variables:

        {
            'version': 3,
            'base_dir': .
            ...
        }

        :param dict vars: dict of variables
        :param dict fields: dict of fields as shown above.