from typing import Union
from pathlib import Path


def relative_to(
    path: Union[str, Path], source: Union[str, Path], include_source: bool = True
) -> Union[str, Path]:
    """Make a path relative to another path.

    In contrast to :meth:`pathlib.Path.relative_to`, this function allows to keep the
    name of the source path.

    Examples
    --------
    The default behavior of :mod:`pathlib` is to exclude the source path from the
    relative path.

    >>> relative_to("folder/file.py", "folder", False).as_posix()
    'file.py'

    To provide relative locations to users, it is sometimes more helpful to provide the
    source as an orientation.

    >>> relative_to("folder/file.py", "folder").as_posix()
    'folder/file.py'

    """
    source_name = Path(source).name if include_source else ""
    return Path(source_name, Path(path).relative_to(source))