from pathlib import Path


def module_name_path(path: Path) -> tuple[str, Path]:
    """Get the module name and path from a path.

    Arguments:
        path: A directory or file path. Paths to `__init__.py` files
            will be resolved to their parent directory.

    Raises:
        FileNotFoundError: When:

            - the directory has no `__init__.py` file in it
            - the path does not exist

    Returns:
        The name of the module (or package) and its path.
    """
    if path.is_dir():
        module_path = path / "__init__.py"
        if module_path.exists():
            return path.name, module_path
        raise FileNotFoundError
    if path.exists():
        if path.stem == "__init__":
            if path.parent.is_absolute():
                return path.parent.name, path
            return path.parent.resolve().name, path
        return path.stem, path
    raise FileNotFoundError