def generate_imports_for_referenced_namespaces(
        backend, namespace, insert_type_ignore=False):
    # type: (Backend, ApiNamespace, bool) -> None
    """
    Both the true Python backend and the Python PEP 484 Type Stub backend have
    to perform the same imports.

    :param insert_type_ignore: add a MyPy type-ignore comment to the imports in
        the except: clause.
    """

    imported_namespaces = namespace.get_imported_namespaces(consider_annotation_types=True)
    if not imported_namespaces:
        return

    type_ignore_comment = TYPE_IGNORE_COMMENT if insert_type_ignore else ""

    backend.emit('try:')
    with backend.indent():
        backend.emit('from . import (')
        with backend.indent():
            for ns in imported_namespaces:
                backend.emit(fmt_namespace(ns.name) + ',')
        backend.emit(')')
    backend.emit('except (ImportError, SystemError, ValueError):')
    # Fallback if imported from outside a package.
    with backend.indent():
        for ns in imported_namespaces:
            backend.emit('import {namespace_name}{type_ignore_comment}'.format(
                namespace_name=fmt_namespace(ns.name),
                type_ignore_comment=type_ignore_comment
            ))
    backend.emit()
def generate_imports_for_referenced_namespaces ( backend , namespace , insert_type_ignore = False ) : imported_namespaces = namespace . get_imported_namespaces ( consider_annotation_types = True ) if not imported_namespaces : return type_ignore_comment = TYPE_IGNORE_COMMENT if insert_type_ignore else "" backend . emit ( 'try:' ) with backend . indent ( ) : backend . emit ( 'from . import (' ) with backend . indent ( ) : for ns in imported_namespaces : backend . emit ( fmt_namespace ( ns . name ) + ',' ) backend . emit ( ')' ) backend . emit ( 'except (ImportError, SystemError, ValueError):' ) with backend . indent ( ) : for ns in imported_namespaces : backend . emit ( 'import {namespace_name}{type_ignore_comment}' . format ( namespace_name = fmt_namespace ( ns . name ) , type_ignore_comment = type_ignore_comment ) ) backend . emit ( )
Both the true Python backend and the Python PEP 484 Type Stub backend have
    to perform the same imports.

    :param insert_type_ignore: add a MyPy type-ignore comment to the imports in
        the except: clause.