from typing import Dict
from typing import Any


def import_name(name: str, source: str, namespace: Dict[str, Any]) -> Any:
    """
    Import ``name`` from ``source`` in ``namespace``.

    There are two use cases:

    - ``name`` is an object defined in ``source``;
    - ``name`` is a submodule of ``source``.

    Neither :func:`__import__` nor :func:`~importlib.import_module` does
    exactly this. :func:`__import__` is closer to the intended behavior.

    """
    level = 0
    while source[level] == ".":
        level += 1
        assert level < len(source), "importing from parent isn't supported"
    module = __import__(source[level:], namespace, None, [name], level)
    return getattr(module, name)