Esempio n. 1
0
 def _resolve_external_and_local_types(self, ast):
     dependencies = self._collect_ast_dependencies(ast)
     if dependencies:
         self._load_ast_dependencies(dependencies, ast)
         ast = self._resolve_external_types(ast)
     ast = ast.Visit(visitors.LookupLocalTypes())
     return ast
Esempio n. 2
0
    def assertTypesMatchPytd(self, ty, pytd_src):
        """Parses pytd_src and compares with ty."""
        pytd_tree = parser.parse_string(
            textwrap.dedent(pytd_src),
            options=parser.PyiOptions(python_version=self.python_version))
        pytd_tree = pytd_tree.Visit(
            visitors.LookupBuiltins(self.loader.builtins, full_names=False))
        pytd_tree = pytd_tree.Visit(visitors.LookupLocalTypes())
        pytd_tree = pytd_tree.Visit(visitors.ClassTypeToNamedType())
        pytd_tree = pytd_tree.Visit(
            visitors.CanonicalOrderingVisitor(sort_signatures=True))
        pytd_tree.Visit(visitors.VerifyVisitor())
        ty = ty.Visit(visitors.ClassTypeToNamedType())
        ty = ty.Visit(visitors.AdjustSelf())
        ty = ty.Visit(visitors.CanonicalOrderingVisitor(sort_signatures=True))
        ty.Visit(visitors.VerifyVisitor())

        ty_src = pytd_utils.Print(ty) + "\n"
        pytd_tree_src = pytd_utils.Print(pytd_tree) + "\n"

        log.info("========== result   ==========")
        _LogLines(log.info, ty_src)
        log.info("========== expected ==========")
        _LogLines(log.info, pytd_tree_src)
        log.info("==============================")

        # In the diff output, mark expected with "-" and actual with "+".
        # (In other words, display a change from "working" to "broken")
        self.assertMultiLineEqual(pytd_tree_src, ty_src)
Esempio n. 3
0
 def _resolve_external_and_local_types(self, pyval, ast=None):
   dependencies = self._collect_ast_dependencies(pyval)
   if dependencies:
     self._load_ast_dependencies(dependencies, ast or pyval)
     pyval = self._resolve_external_types(pyval, ast and ast.name)
   local_lookup = visitors.LookupLocalTypes()
   if ast:
     local_lookup.EnterTypeDeclUnit(ast)
   pyval = pyval.Visit(local_lookup)
   return pyval
Esempio n. 4
0
 def _postprocess_pyi(self, ast):
   """Apply all the PYI transformations we need."""
   ast = ast.Visit(visitors.LookupBuiltins(self.builtins, full_names=False))
   ast = ast.Visit(visitors.ExpandCompatibleBuiltins(self.builtins))
   dependencies = self._collect_ast_dependencies(ast)
   if dependencies:
     self._load_ast_dependencies(dependencies, ast)
     ast = self._resolve_external_types(ast)
   ast = ast.Visit(visitors.LookupLocalTypes())
   return ast
Esempio n. 5
0
 def _postprocess_pyi(self, ast):
     """Apply all the PYI transformations we need."""
     package_name = module_utils.get_package_name(ast.name, ast.is_package)
     if package_name:
         ast = ast.Visit(visitors.QualifyRelativeNames(package_name))
     ast = ast.Visit(
         visitors.LookupBuiltins(self.builtins, full_names=False))
     ast = ast.Visit(visitors.ExpandCompatibleBuiltins(self.builtins))
     dependencies = self._collect_ast_dependencies(ast)
     if dependencies:
         self._load_ast_dependencies(dependencies, ast)
         ast = self._resolve_external_types(ast)
     ast = ast.Visit(visitors.LookupLocalTypes())
     return ast
Esempio n. 6
0
def PrepareForExport(module_name, python_version, ast, loader):
    """Prepare an ast as if it was parsed and loaded.

  External dependencies will not be resolved, as the ast generated by this
  method is supposed to be exported.

  Args:
    module_name: The module_name as a string for the returned ast.
    python_version: A tuple of (major, minor) python version as string
      (see config.python_version).
    ast: pytd.TypeDeclUnit, is only used if src is None.
    loader: A load_pytd.Loader instance.

  Returns:
    A pytd.TypeDeclUnit representing the supplied AST as it would look after
    being written to a file and parsed.
  """
    # This is a workaround for functionality which crept into places it doesn't
    # belong. Ideally this would call some transformation Visitors on ast to
    # transform it into the same ast we get after parsing and loading (compare
    # load_pytd.Loader.load_file). Unfortunately parsing has some special cases,
    # e.g. '__init__' return type and '__new__' being a 'staticmethod', which
    # need to be moved to visitors before we can do this. Printing an ast also
    # applies transformations,
    # e.g. visitors.PrintVisitor._FormatContainerContents, which need to move to
    # their own visitors so they can be applied without printing.
    src = pytd_utils.Print(ast)
    ast = parser.parse_string(src=src,
                              name=module_name,
                              python_version=python_version)
    ast = ast.Visit(visitors.LookupBuiltins(loader.builtins, full_names=False))
    ast = ast.Visit(
        visitors.ExpandCompatibleBuiltins(loader.builtins, python_version))
    ast = ast.Visit(visitors.LookupLocalTypes())
    ast = ast.Visit(visitors.AdjustTypeParameters())
    ast = ast.Visit(visitors.NamedTypeToClassType())
    ast = ast.Visit(visitors.FillInLocalPointers({"": ast, module_name: ast}))
    ast = ast.Visit(visitors.CanonicalOrderingVisitor())
    ast = ast.Visit(
        visitors.ClassTypeToLateType(
            ignore=[module_name + ".", "__builtin__.", "typing."]))
    return ast
Esempio n. 7
0
 def resolve_local_types(self, mod_ast, *, lookup_ast=None):
   local_lookup = visitors.LookupLocalTypes()
   return self._lookup(local_lookup, mod_ast, lookup_ast)