from typing import Dict


def merge_hooks(hooks1: Dict[str, list], hooks2: Dict[str, list]) -> Dict[str, list]:
    """
    Overview:
        merge two hooks, which has the same keys, each value is sorted by hook priority with stable method
    Arguments:
        - hooks1 (:obj:`dict`): hooks1 to be merged
        - hooks2 (:obj:`dict`): hooks2 to be merged
    Returns:
        - new_hooks (:obj:`dict`): merged new hooks

    .. note::
        This merge function uses stable sort method without disturbing the same priority hook
    """
    assert set(hooks1.keys()) == set(hooks2.keys())
    new_hooks = {}
    for k in hooks1.keys():
        new_hooks[k] = sorted(hooks1[k] + hooks2[k], key=lambda x: x.priority)
    return new_hooks