from typing import Union
from typing import List
from typing import Type
import importlib
import inspect


def find_subclass(module_path: Union[str, List[str]], base_class: Type) -> Type:
    """ Get child class from module

    :param Union[str, List[str]] module_path: module path or list of paths in dot-notation
    :param Type base_class: class the child class inherits from
    :return: imported child class
    :raise ImportError: module path not valid
    :raise StopIteration: no class found
    """
    if isinstance(module_path, str):
        module_path = [module_path]
    errors = ''
    module = None
    for path in module_path:
        try:
            module = importlib.import_module(path)
            break
        except ImportError as e:
            errors += '\n' + str(e)
    if module is None:
        raise ImportError(
            'Unable to load any module from {}: {}'
            .format(module_path, errors),
        )
    return next(
        obj for _, obj in inspect.getmembers(module)
        if inspect.isclass(obj)
        and issubclass(obj, base_class)
        and obj != base_class
    )