from pathlib import Path


def get_rel_path(dirpath: Path, basepath: Path = Path().resolve()):
    """
    Get dirpath as relative to basepath. This handles corner cases better than
    Path.relative_to, uses ".." path notation so that the relative path is
    always obtained no matter where the two paths are located. Here, dirpath
    and basepath must be fully resolved absolute paths to directories. By
    default, basepath is current working dir
    """
    ret_parts: list[str] = []
    back_parts = 0
    has_seperated = False

    cnt = -1
    for cnt, part in enumerate(dirpath.parts):
        if cnt >= len(basepath.parts):
            ret_parts.append(part)
            continue

        # got a mismatching parent dir
        if basepath.parts[cnt] != part:
            has_seperated = True

        if has_seperated:
            ret_parts.append(part)
            back_parts += 1

    cnt += 1
    back_parts += max(len(basepath.parts) - cnt, 0)

    ret = Path(*([".."] * back_parts))
    for part in ret_parts:
        ret /= part

    return ret