Beispiel #1
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)
Beispiel #2
0
def main():
  argument_parser = make_parser()
  opts = argument_parser.parse_args()
  if opts.python_version:
    python_version = utils.version_from_string(opts.python_version)
  else:
    python_version = sys.version_info[:2]
  try:
    utils.validate_version(python_version)
  except utils.UsageError as e:
    sys.stderr.write("Usage error: %s\n" % utils.message(e))
    sys.exit(1)

  options = parser.PyiOptions(python_version=python_version)

  with open(opts.input) as fi:
    sourcecode = fi.read()
    try:
      parsed = parser.parse_string(
          sourcecode, filename=opts.input, options=options)
    except parser.ParseError as e:
      sys.stderr.write(str(e))
      sys.exit(1)

  if opts.optimize:
    parsed = optimize.Optimize(
        parsed,
        pytd_utils.Concat(*builtin_stubs.GetBuiltinsAndTyping(options)),
        lossy=opts.lossy,
        use_abcs=opts.use_abcs,
        max_union=opts.max_union,
        remove_mutable=opts.remove_mutable,
        can_do_lookup=False)

  if opts.output is not None:
    out_text = pytd_utils.Print(parsed, opts.multiline_args)
    if opts.output == "-":
      sys.stdout.write(out_text)
    else:
      with open(opts.output, "w") as out:
        out.write(out_text)
Beispiel #3
0
 def setUp(self):
   super().setUp()
   self.options = parser.PyiOptions(python_version=self.python_version)
 def setUpClass(cls):
     super(UtilsTest, cls).setUpClass()
     cls.builtins = pytd_utils.Concat(*builtin_stubs.GetBuiltinsAndTyping(
         parser.PyiOptions(python_version=cls.python_version)))
Beispiel #5
0
 def _parse_string(self, string):
     """A wrapper for parser.parse_string that inserts the python version."""
     return parser.parse_string(
         string,
         options=parser.PyiOptions(python_version=self.python_version))