# Buggy code
def require(source_module, target_module, assignments, prefix=""):
    """Load macros from one module into the namespace of another.

    This function is called from the `require` special form in the compiler.

    Parameters
    ----------
    source_module: str or types.ModuleType
        The module from which macros are to be imported.

    target_module: str, types.ModuleType or None
        The module into which the macros will be loaded.  If `None`, then
        the caller's namespace.
        The latter is useful during evaluation of generated AST/bytecode.

    assignments: str or list of tuples of strs
        The string "ALL" or a list of macro name and alias pairs.

    prefix: str, optional ("")
        If nonempty, its value is prepended to the name of each imported macro.
        This allows one to emulate namespaced macros, like
        "mymacromodule.mymacro", which looks like an attribute of a module.

    Returns
    -------
    out: boolean
        Whether or not macros and tags were actually transferred.
    """

    if target_module is None:
        parent_frame = inspect.stack()[1][0]
        target_namespace = parent_frame.f_globals
        target_module = target_namespace.get("__name__", None)
    elif isinstance(target_module, string_types):
        target_module = importlib.import_module(target_module)
        target_namespace = target_module.__dict__
    elif inspect.ismodule(target_module):
        target_namespace = target_module.__dict__
    else:
        raise TypeError(
            "`target_module` is not a recognized type: {}".format(type(target_module))
        )

    # Let's do a quick check to make sure the source module isn't actually
    # the module being compiled (e.g. when `runpy` executes a module's code
    # in `__main__`).
    # We use the module's underlying filename for this (when they exist), since
    # it's the most "fixed" attribute.
    if _same_modules(source_module, target_module):
        return False

    if not inspect.ismodule(source_module):
        source_module = importlib.import_module(source_module)

    source_macros = source_module.__dict__.setdefault("__macros__", {})
    source_tags = source_module.__dict__.setdefault("__tags__", {})

    if len(source_module.__macros__) + len(source_module.__tags__) == 0:
        if assignments != "ALL":
            raise ImportError(
                "The module {} has no macros or tags".format(source_module)
            )
        else:
            return False

    target_macros = target_namespace.setdefault("__macros__", {})
    target_tags = target_namespace.setdefault("__tags__", {})

    if prefix:
        prefix += "."

    if assignments == "ALL":
        name_assigns = [
            (k, k) for k in tuple(source_macros.keys()) + tuple(source_tags.keys())
        ]
    else:
        name_assigns = assignments

    for name, alias in name_assigns:
        _name = mangle(name)
        alias = mangle(prefix + alias)
        if _name in source_module.__macros__:
            target_macros[alias] = source_macros[_name]
        elif _name in source_module.__tags__:
            target_tags[alias] = source_tags[_name]
        else:
            raise ImportError(
                "Could not require name {} from {}".format(_name, source_module)
            )

    return True


# Traceback (hy.errors.HyCompileError)
Traceback (most recent call last):
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/importer.py", line 180, in hy_eval
_ast, expr = hy_compile(hytree, module_name, get_expr=True)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 2213, in hy_compile
result = compiler.compile(tree)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
ret = self.compile_atom(_type, tree)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
ret = self.compile_atom(fn, expression)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 819, in compile_do
return self._compile_branch(expression)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in _compile_branch
return _branch(self.compile(expr) for expr in exprs)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 318, in _branch
results = list(results)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in <genexpr>
return _branch(self.compile(expr) for expr in exprs)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
ret = self.compile_atom(_type, tree)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
ret = self.compile_atom(fn, expression)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 372, in checker
return fn(self, expression)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 873, in compile_try_expression
name)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1002, in _compile_catch_expression
body = self._compile_branch(expr)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in _compile_branch
return _branch(self.compile(expr) for expr in exprs)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 318, in _branch
results = list(results)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in <genexpr>
return _branch(self.compile(expr) for expr in exprs)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
ret = self.compile_atom(_type, tree)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
ret = self.compile_atom(fn, expression)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 372, in checker
return fn(self, expression)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 858, in compile_try_expression
body += self.compile(expr.pop(0))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 472, in compile
raise_empty(HyCompileError, e, sys.exc_info()[2])
File "<string>", line 1, in raise_empty
hy.errors.HyCompileError: Internal Compiler Bug 😱
⤷ ImportError: cannot require names: [HySymbol('let')]
Compilation traceback:
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
ret = self.compile_atom(_type, tree)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
ret = self.compile_atom(fn, expression)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
else _compile_table[atom_type](self, atom))
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1486, in compile_require
require(module, self.module_name, assignments=assignments)
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/macros.py", line 106, in require
raise ImportError("cannot require names: " + repr(list(unseen)))

# Fixed code
def require(source_module, target_module, assignments, prefix=""):
    """Load macros from one module into the namespace of another.

    This function is called from the `require` special form in the compiler.

    Parameters
    ----------
    source_module: str or types.ModuleType
        The module from which macros are to be imported.

    target_module: str, types.ModuleType or None
        The module into which the macros will be loaded.  If `None`, then
        the caller's namespace.
        The latter is useful during evaluation of generated AST/bytecode.

    assignments: str or list of tuples of strs
        The string "ALL" or a list of macro name and alias pairs.

    prefix: str, optional ("")
        If nonempty, its value is prepended to the name of each imported macro.
        This allows one to emulate namespaced macros, like
        "mymacromodule.mymacro", which looks like an attribute of a module.

    Returns
    -------
    out: boolean
        Whether or not macros and tags were actually transferred.
    """
    if target_module is None:
        parent_frame = inspect.stack()[1][0]
        target_namespace = parent_frame.f_globals
        target_module = target_namespace.get("__name__", None)
    elif isinstance(target_module, string_types):
        target_module = importlib.import_module(target_module)
        target_namespace = target_module.__dict__
    elif inspect.ismodule(target_module):
        target_namespace = target_module.__dict__
    else:
        raise HyTypeError(
            "`target_module` is not a recognized type: {}".format(type(target_module))
        )

    # Let's do a quick check to make sure the source module isn't actually
    # the module being compiled (e.g. when `runpy` executes a module's code
    # in `__main__`).
    # We use the module's underlying filename for this (when they exist), since
    # it's the most "fixed" attribute.
    if _same_modules(source_module, target_module):
        return False

    if not inspect.ismodule(source_module):
        try:
            source_module = importlib.import_module(source_module)
        except ImportError as e:
            reraise(HyRequireError, HyRequireError(e.args[0]), None)

    source_macros = source_module.__dict__.setdefault("__macros__", {})
    source_tags = source_module.__dict__.setdefault("__tags__", {})

    if len(source_module.__macros__) + len(source_module.__tags__) == 0:
        if assignments != "ALL":
            raise HyRequireError(
                "The module {} has no macros or tags".format(source_module)
            )
        else:
            return False

    target_macros = target_namespace.setdefault("__macros__", {})
    target_tags = target_namespace.setdefault("__tags__", {})

    if prefix:
        prefix += "."

    if assignments == "ALL":
        name_assigns = [
            (k, k) for k in tuple(source_macros.keys()) + tuple(source_tags.keys())
        ]
    else:
        name_assigns = assignments

    for name, alias in name_assigns:
        _name = mangle(name)
        alias = mangle(prefix + alias)
        if _name in source_module.__macros__:
            target_macros[alias] = source_macros[_name]
        elif _name in source_module.__tags__:
            target_tags[alias] = source_tags[_name]
        else:
            raise HyRequireError(
                "Could not require name {} from {}".format(_name, source_module)
            )

    return True