Example #1
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 #2
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 #3
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 #4
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 #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:
             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 #6
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 #7
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_top_level = self.is_global_scope
     super().visit_import_from(node)
Example #8
0
 def visit_import_from(self, node: ImportFrom) -> None:
     node.is_top_level = self.is_global_scope
     super().visit_import_from(node)