def compress_for_rename(paths: Iterable[NormalizedPath]) -> set[NormalizedPath]:
    """Returns a set containing the paths that need to be renamed.

    This set may include directories when the original sequence of paths
    included every file on disk.
    """
    case_map = {NormalizedPath(os.path.normcase(p)): p for p in paths if os.path.exists(p)}
    remaining = set(case_map)
    unchecked = sorted({NormalizedPath(os.path.split(p)[0]) for p in case_map.values()}, key=len)
    wildcards: set[NormalizedPath] = set()

    def norm_join(*a: str) -> NormalizedPath:
        return NormalizedPath(os.path.normcase(os.path.join(*a)))

    for root in unchecked:
        if any(os.path.normcase(root).startswith(w) for w in wildcards):
            # This directory has already been handled.
            continue

        all_files: set[NormalizedPath] = set()
        for dirname, subdirs, files in os.walk(root):
            all_files.update(norm_join(root, dirname, f) for f in files)
            for d in subdirs:
                norm_path = norm_join(root, dirname, d)
                if os.path.islink(norm_path):
                    all_files.add(norm_path)

        # If all the files we found are in our remaining set of files to
        # remove, then remove them from the latter set and add a wildcard
        # for the directory.
        if not (all_files - remaining):
            remaining.difference_update(all_files)
            wildcards.add(NormalizedPath(root + os.sep))

    collected = set(map(case_map.__getitem__, remaining)) | wildcards
    shortened: set[NormalizedPath] = set()
    # Filter out any paths that are sub paths of another path in the path collection.
    for path in sorted(collected, key=len):
        if not any(is_path_relative_to(path, p) for p in shortened):
            shortened.add(path)
    return shortened
Returns a set containing the paths that need to be renamed.

    This set may include directories when the original sequence of paths
    included every file on disk.
    
src/pdm/installers/uninstallers.py