def _override_imports( file: MypyFile, module: str, imports: t.List[t.Tuple[str, t.Optional[str]]], ) -> None: """Override the first `module`-based import with new `imports`.""" # Construct a new `from module import y` statement import_obj = ImportFrom(module, 0, names=imports) import_obj.is_top_level = True # Replace the first `module`-based import statement with `import_obj` for lst in [file.defs, file.imports]: # type: t.List[Statement] i = _index(lst, module) lst[i] = import_obj
def visit_ImportFrom(self, n): if len(n.names) == 1 and n.names[0].name == '*': i = ImportAll(n.module, n.level) else: i = ImportFrom(n.module if n.module is not None else '', n.level, [(a.name, a.asname) for a in n.names]) self.imports.append(i) return i
def visit_ImportFrom(self, n: ast35.ImportFrom) -> Node: i = None # type: ImportBase if len(n.names) == 1 and n.names[0].name == '*': i = ImportAll(n.module, n.level) else: i = ImportFrom(self.translate_module_id(n.module) if n.module is not None else '', n.level, [(a.name, a.asname) for a in n.names]) self.imports.append(i) return i
def visit_ImportFrom(self, n: ast27.ImportFrom) -> ImportBase: assert n.level is not None if len(n.names) == 1 and n.names[0].name == '*': mod = n.module if n.module is not None else '' i = ImportAll(mod, n.level) # type: ImportBase else: i = ImportFrom(self.translate_module_id(n.module) if n.module is not None else '', n.level, [(a.name, a.asname) for a in n.names]) self.imports.append(i) return i
def visit_import_from(self, node: ImportFrom) -> None: # We can't bind module names during the first pass, as the target module might be # unprocessed. However, we add dummy unbound imported names to the symbol table so # that we at least know that the name refers to a module. at_module = self.sem.is_module_scope() node.is_top_level = at_module if not at_module: return for name, as_name in node.names: imported_name = as_name or name if imported_name not in self.sem.globals: self.sem.add_symbol(imported_name, SymbolTableNode(UNBOUND_IMPORTED, None), node)
def visit_import_from(self, node: ImportFrom) -> None: # We can't bind module names during the first pass, as the target module might be # unprocessed. However, we add dummy unbound imported names to the symbol table so # that we at least know that the name refers to a module. at_module = self.sem.is_module_scope() node.is_top_level = at_module if not at_module: return for name, as_name in node.names: imported_name = as_name or name if imported_name not in self.sem.globals: self.add_symbol(imported_name, SymbolTableNode(UNBOUND_IMPORTED, None), node)
def visit_import_from(self, node: ImportFrom) -> None: if node.assignments: node.assignments = [] else: if self.names: # Reset entries in the symbol table. This is necessary since # otherwise the semantic analyzer will think that the import # assigns to an existing name instead of defining a new one. for name, as_name in node.names: imported_name = as_name or name symnode = self.names[imported_name] symnode.kind = UNBOUND_IMPORTED symnode.node = None
def visit_import_from(self, node: ImportFrom) -> None: if node.assignments: node.assignments = [] else: if self.names: # Reset entries in the symbol table. This is necessary since # otherwise the semantic analyzer will think that the import # assigns to an existing name instead of defining a new one. for name, as_name in node.names: imported_name = as_name or name symnode = self.names[imported_name] symnode.kind = UNBOUND_IMPORTED symnode.node = None
def visit_ImportFrom(self, n: ast27.ImportFrom) -> ImportBase: assert n.level is not None if len(n.names) == 1 and n.names[0].name == '*': mod = n.module if n.module is not None else '' i = ImportAll(mod, n.level) # type: ImportBase else: module_id = self.translate_module_id(n.module) if n.module is not None else '' i = ImportFrom(module_id, n.level, [(a.name, a.asname) for a in n.names]) # See comments in the constructor for more information about this field. if module_id == '__future__' and any(a.name == 'unicode_literals' for a in n.names): self.unicode_literals = True self.imports.append(i) return self.set_line(i, n)
def get_additional_deps( self, file: MypyFile) -> t.List[t.Tuple[int, str, int]]: """Import platform-specific extended-precision `numpy.number` subclasses. For example: `numpy.float96`, `numpy.float128` and `numpy.complex256`. """ ret = [(PRI_MED, file.fullname, -1)] if file.fullname == "numpy": # Import ONLY the extended precision types available to the # platform in question imports = ImportFrom( "numpy.typing._extended_precision", 0, names=[(v, v) for v in _EXTENDED_PRECISION_LIST], ) imports.is_top_level = True # Replace the much broader extended-precision import # (defined in `numpy/__init__.pyi`) with a more specific one for lst in [file.defs, file.imports]: # type: t.List[Statement] i = _index(lst, "numpy.typing._extended_precision") lst[i] = imports return ret
def visit_import_from(self, node: ImportFrom) -> None: # We can't bind module names during the first pass, as the target module might be # unprocessed. However, we add dummy unbound imported names to the symbol table so # that we at least know that the name refers to a module. at_module = self.sem.is_module_scope() node.is_top_level = at_module if not at_module: return for name, as_name in node.names: imported_name = as_name or name if imported_name not in self.sem.globals: sym = create_indirect_imported_name(self.sem.cur_mod_node, node.id, node.relative, name) if sym: self.add_symbol(imported_name, sym, context=node)
def visit_import_from(self, node: ImportFrom) -> None: # We can't bind module names during the first pass, as the target module might be # unprocessed. However, we add dummy unbound imported names to the symbol table so # that we at least know that the name refers to a module. at_module = self.sem.is_module_scope() node.is_top_level = at_module if not at_module: return for name, as_name in node.names: imported_name = as_name or name if imported_name not in self.sem.globals: sym = create_indirect_imported_name(self.sem.cur_mod_node, node.id, node.relative, name) if sym: self.add_symbol(imported_name, sym, context=node)
def visit_import_from(self, node: ImportFrom) -> None: if node.assignments: node.assignments = [] else: # If the node is unreachable, don't reset entries: they point to something else! if node.is_unreachable: return if self.names: # Reset entries in the symbol table. This is necessary since # otherwise the semantic analyzer will think that the import # assigns to an existing name instead of defining a new one. for name, as_name in node.names: imported_name = as_name or name # This assert is safe since we check for self.names above. assert self.file_node is not None sym = create_indirect_imported_name( self.file_node, node.id, node.relative, name) if sym: self.names[imported_name] = sym
def visit_import_from(self, node: ImportFrom) -> None: # Imports can include both overriding symbols and fresh ones, # and we need to clear both. node.assignments = [] # If the node is unreachable, don't reset entries: they point to something else! if node.is_unreachable: return if self.names: # Reset entries in the symbol table. This is necessary since # otherwise the semantic analyzer will think that the import # assigns to an existing name instead of defining a new one. for name, as_name in node.names: imported_name = as_name or name # This assert is safe since we check for self.names above. assert self.file_node is not None sym = create_indirect_imported_name(self.file_node, node.id, node.relative, name) if sym: self.names[imported_name] = sym
def visit_import_from(self, node: ImportFrom) -> None: node.is_unreachable = True
def visit_import_from(self, node: ImportFrom) -> Node: return ImportFrom(node.id, node.relative, node.names[:])
def visit_import_from(self, node: ImportFrom) -> None: node.assignments = []
def visit_import_from(self, node: ImportFrom) -> None: node.is_mypy_only = True
def visit_import_from(self, node: ImportFrom) -> None: node.is_top_level = self.is_global_scope super().visit_import_from(node)
def visit_import_from(self, node: ImportFrom) -> None: node.is_mypy_only = True
def visit_import_from(self, node: ImportFrom) -> None: node.is_unreachable = True
def visit_import_from(self, node: ImportFrom) -> None: node.is_top_level = self.is_global_scope super().visit_import_from(node)
def visit_import_from(self, node: ImportFrom) -> None: node.assignments = []