def _normalize_target_module(source_module, target_module, level):
    """ Normalize relative import, to absolute import if possible.

    Parameters
    ----------
    source_module : str or None
        Name of the module where the import is written.
        If given, this name should be absolute.
    target_module : str
        Name of the module being imported.
        This name can be absolute or relative depending on the value
        of ``level``.
    level : int, optional
        level specifies whether to use absolute or relative
        imports. 0 (the default) means only perform absolute
        imports. Positive values for level indicate the number
        of parent directories to search relative to the directory
        of the module calling import.

    Returns
    -------
    module_name : str
        Normalized import name
    """
    if source_module is None or level == 0:
        return target_module

    source_parts = source_module.split(".")
    if level > len(source_parts):
        raise ValueError("Level is too deep.")
    target_parts = source_parts[:-level] + target_module.split(".")
    return ".".join(target_parts)