Example #1
0
    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
Example #2
0
 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
Example #3
0
 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
Example #4
0
 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
Example #5
0
 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)
Example #6
0
 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)
Example #7
0
 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
Example #8
0
 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
Example #9
0
    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)
Example #10
0
        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
Example #11
0
 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)
Example #12
0
 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)
Example #13
0
 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
Example #14
0
    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
Example #15
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_unreachable = True
Example #16
0
 def visit_import_from(self, node: ImportFrom) -> Node:
     return ImportFrom(node.id, node.relative, node.names[:])
Example #17
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.assignments = []
Example #18
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_mypy_only = True
Example #19
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_top_level = self.is_global_scope
     super().visit_import_from(node)
Example #20
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_mypy_only = True
Example #21
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_unreachable = True
Example #22
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_top_level = self.is_global_scope
     super().visit_import_from(node)
Example #23
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.assignments = []