# Buggy code
def _expand_yield_statements(macro_def, expand_el):
    yield_els = [
        yield_el
        for macro_def_el in macro_def
        for yield_el in macro_def_el.findall(".//yield")
    ]

    expand_el_children = list(expand_el)
    macro_def_parent_map = dict(
        (c, p) for macro_def_el in macro_def for p in macro_def_el.iter() for c in p
    )

    for yield_el in yield_els:
        _xml_replace(yield_el, expand_el_children, macro_def_parent_map)

    # Replace yields at the top level of a macro, seems hacky approach
    replace_yield = True
    while replace_yield:
        for i, macro_def_el in enumerate(macro_def):
            if macro_def_el.tag == "yield":
                for target in expand_el_children:
                    i += 1
                    macro_def.insert(i, target)
                macro_def.remove(macro_def_el)
                continue

        replace_yield = False


# Traceback (AssertionError)
galaxy.tools.toolbox.base ERROR 2019-10-15 10:55:57,876 Failed to load potential tool /Users/scholtalbers/workspace/je-suite/src/galaxy/je-clip.xml.
Traceback (most recent call last):
File "/private/var/folders/cf/k7c5rjhj1sggk6x8z1jnw4b80000gp/T/tmpFw8KvW/galaxy-dev/lib/galaxy/tools/toolbox/base.py", line 772, in quick_load
tool = self.load_tool(tool_file)
File "/private/var/folders/cf/k7c5rjhj1sggk6x8z1jnw4b80000gp/T/tmpFw8KvW/galaxy-dev/lib/galaxy/tools/toolbox/base.py", line 808, in load_tool
tool = self.create_tool(config_file=config_file, tool_shed_repository=tool_shed_repository, guid=guid, **kwds)
File "/private/var/folders/cf/k7c5rjhj1sggk6x8z1jnw4b80000gp/T/tmpFw8KvW/galaxy-dev/lib/galaxy/tools/__init__.py", line 274, in create_tool
raise e
AssertionError
galaxy.tools.toolbox.base ERROR 2019-10-15 10:55:57,887 Failed to load potential tool /Users/scholtalbers/workspace/je-suite/src/galaxy/je-demultiplex.xml.
Traceback (most recent call last):
File "/private/var/folders/cf/k7c5rjhj1sggk6x8z1jnw4b80000gp/T/tmpFw8KvW/galaxy-dev/lib/galaxy/tools/toolbox/base.py", line 772, in quick_load
tool = self.load_tool(tool_file)
File "/private/var/folders/cf/k7c5rjhj1sggk6x8z1jnw4b80000gp/T/tmpFw8KvW/galaxy-dev/lib/galaxy/tools/toolbox/base.py", line 808, in load_tool
tool = self.create_tool(config_file=config_file, tool_shed_repository=tool_shed_repository, guid=guid, **kwds)
File "/private/var/folders/cf/k7c5rjhj1sggk6x8z1jnw4b80000gp/T/tmpFw8KvW/galaxy-dev/lib/galaxy/tools/__init__.py", line 274, in create_tool
raise e
AssertionError

# Fixed code
def _expand_yield_statements(macro_def, expand_el):
    yield_els = [
        yield_el
        for macro_def_el in macro_def
        for yield_el in macro_def_el.findall(".//yield")
    ]

    expand_el_children = list(expand_el)
    macro_def_parent_map = dict(
        (c, p) for macro_def_el in macro_def for p in macro_def_el.iter() for c in p
    )

    for yield_el in yield_els:
        _xml_replace(yield_el, expand_el_children, macro_def_parent_map)

    # Replace yields at the top level of a macro, seems hacky approach
    replace_yield = True
    while replace_yield:
        for i, macro_def_el in enumerate(macro_def):
            if macro_def_el.tag == "yield":
                for target in expand_el_children:
                    i += 1
                    macro_def.insert(i, deepcopy(target))
                macro_def.remove(macro_def_el)
                continue

        replace_yield = False