예제 #1
0
 def _finish_ast(self, ast):
     module_map = {
         name: module.ast
         for name, module in self._modules.items()
     }
     module_map[""] = ast  # The module itself (local lookup)
     ast.Visit(visitors.FillInModuleClasses(module_map))
예제 #2
0
 def LinkAgainstSimpleBuiltins(self, ast):
     ast = ast.Visit(visitors.NamedTypeToClassType())
     ast = visitors.AdjustTypeParameters(ast)
     ast.Visit(visitors.FillInModuleClasses({"": ast}))
     ast = ast.Visit(visitors.LookupFullNames([self.mini_builtins]))
     ast.Visit(visitors.VerifyLookup())
     return ast
예제 #3
0
 def ParseWithBuiltins(self, src):
   ast = parser.TypeDeclParser().Parse(textwrap.dedent(src))
   b, t = builtins.GetBuiltinsAndTyping()
   ast = ast.Visit(visitors.LookupExternalTypes(
       {"__builtin__": b, "typing": t}, full_names=True))
   ast = ast.Visit(visitors.NamedTypeToClassType())
   ast = visitors.AdjustTypeParameters(ast)
   ast.Visit(visitors.FillInModuleClasses({"": ast}))
   ast.Visit(visitors.VerifyVisitor())
   return ast
예제 #4
0
파일: builtins.py 프로젝트: runt18/pytype
def GetBuiltinsAndTyping():
    """Get __builtin__.pytd and typing.pytd."""
    global _cached_builtins_pytd
    if not _cached_builtins_pytd:
        t = parser.TypeDeclParser().Parse(_FindBuiltinFile("typing"),
                                          name="typing")
        t = t.Visit(visitors.AddNamePrefix())
        b = parser.TypeDeclParser().Parse(_FindBuiltinFile("__builtin__"),
                                          name="__builtin__")
        b = b.Visit(visitors.AddNamePrefix())
        b = b.Visit(visitors.NamedTypeToClassType())
        b = b.Visit(
            visitors.LookupExternalTypes({"typing": t},
                                         full_names=True,
                                         self_name="__builtin__"))
        t = t.Visit(visitors.LookupBuiltins(b))
        t = t.Visit(visitors.NamedTypeToClassType())
        b = visitors.AdjustTypeParameters(b)
        t = visitors.AdjustTypeParameters(t)
        b.Visit(
            visitors.FillInModuleClasses({
                "": b,
                "typing": t,
                "__builtin__": b
            }))
        t.Visit(
            visitors.FillInModuleClasses({
                "": t,
                "typing": t,
                "__builtin__": b
            }))
        b.Visit(visitors.VerifyLookup())
        t.Visit(visitors.VerifyLookup())
        b.Visit(visitors.VerifyContainers())
        t.Visit(visitors.VerifyContainers())
        _cached_builtins_pytd = b, t
    return _cached_builtins_pytd
예제 #5
0
 def _load_file(self, module_name, filename, ast=None):
     """Load (or retrieve from cache) a module and resolve its dependencies."""
     self._concatenated = None  # invalidate
     existing = self._modules.get(module_name)
     if existing:
         if existing.filename != filename:
             raise AssertionError(
                 "%s exists as both %s and %s" %
                 (module_name, filename, existing.filename))
         return existing.ast
     if not ast:
         ast = builtins.ParsePyTD(
             filename=filename,
             module=module_name,
             python_version=self.options.python_version)
     ast = self._postprocess_pyi(ast)
     module = Module(module_name, filename, ast)
     self._modules[module_name] = module
     try:
         module.ast = self._load_and_resolve_ast_dependencies(
             module.ast, module_name)
         # Now that any imported TypeVar instances have been resolved, adjust type
         # parameters in classes and functions.
         module.ast = visitors.AdjustTypeParameters(module.ast)
         # Now we can fill in internal cls pointers to ClassType nodes in the
         # module. This code executes when the module is first loaded, which
         # happens before any others use it to resolve dependencies, so there are
         # no external pointers into the module at this point.
         module.ast.Visit(
             visitors.FillInModuleClasses({
                 "": module.ast,
                 module_name: module.ast
             }))
         # TODO(rechen): Once generics are supported in inline type annotations, a
         # VerifyContainers check should also be done on the final ast.
         module.ast.Visit(visitors.VerifyContainers())
     except:
         del self._modules[
             module_name]  # don't leave half-resolved modules around
         raise
     return module.ast