Exemple #1
0
    def assertTypesMatchPytd(self, ty, pytd_src, version=None):
        """Parses pytd_src and compares with ty."""
        pytd_tree = parser.parse_string(textwrap.dedent(pytd_src),
                                        python_version=version)
        pytd_tree = pytd_tree.Visit(
            visitors.LookupBuiltins(builtins.GetBuiltinsAndTyping()[0],
                                    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(force=True))
        ty = ty.Visit(visitors.CanonicalOrderingVisitor(sort_signatures=True))
        ty.Visit(visitors.VerifyVisitor())

        ty_src = pytd.Print(ty) + "\n"
        pytd_tree_src = pytd.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)
Exemple #2
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))
     ast = ast.Visit(visitors.LookupLocalTypes())
     return ast
Exemple #3
0
    def assertTypesMatchPytd(self, ty, pytd_src, version=None):
        """Parses pytd_src and compares with ty."""
        # TODO(pludemann): This is a copy of pytd.parse.parser_test_base.Parse()
        # TODO(pludemann): Consider using the pytd_tree to call
        #                  assertHasOnlySignatures (or similar) to guard against the
        #                  inferencer adding additional but harmless calls.
        pytd_tree = parser.parse_string(textwrap.dedent(pytd_src),
                                        python_version=version)
        pytd_tree = pytd_tree.Visit(
            visitors.LookupBuiltins(builtins.GetBuiltinsAndTyping()[0],
                                    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(force=True))
        ty = ty.Visit(visitors.CanonicalOrderingVisitor(sort_signatures=True))
        ty.Visit(visitors.VerifyVisitor())

        ty_src = pytd.Print(ty) + "\n"
        pytd_tree_src = pytd.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)
Exemple #4
0
 def _postprocess_pyi(self, ast, package_name):
     """Apply all the PYI transformations we need."""
     if package_name is not None:
         ast = ast.Visit(visitors.QualifyRelativeNames(package_name))
     ast = ast.Visit(
         visitors.LookupBuiltins(self.builtins, full_names=False))
     ast = ast.Visit(visitors.ExpandCompatibleBuiltins(self.builtins))
     ast = ast.Visit(visitors.LookupLocalTypes())
     return ast
Exemple #5
0
 def _postprocess_pyi(self, ast):
   """Apply all the PYI transformations we need."""
   package_name = utils.get_pyi_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
Exemple #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 = pytd_builtins.ParsePyTD(src=src,
                                  module=module_name,
                                  python_version=python_version)
    ast = ast.Visit(visitors.LookupBuiltins(loader.builtins, full_names=False))
    ast = ast.Visit(visitors.ExpandCompatibleBuiltins(loader.builtins))
    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