Beispiel #1
0
 def InferAndCheck(self, code):
   errorlog = errors.ErrorLog()
   unit = infer.infer_types(
       textwrap.dedent(code), self.PYTHON_VERSION, errorlog, deep=True,
       reverse_operators=True, cache_unknowns=True)
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit), errorlog
Beispiel #2
0
  def _InferAndVerify(self, src, pythonpath=(), module_name=None,
                      imports_map=None, report_errors=False, quick=False,
                      **kwargs):
    """Infer types for the source code treating it as a module.

    Used by Infer().

    Args:
      src: The source code of a module. Treat it as "__main__".
      pythonpath: --pythonpath as list/tuple of string
      module_name: Name of the module we're analyzing. E.g. "foo.bar.mymodule".
      imports_map: --imports_info data
      report_errors: Whether to fail if the type inferencer reports any errors
        in the program.
      quick: Try to run faster, by avoiding costly computations.
      **kwargs: Keyword paramters to pass through to the type inferencer.

    Raises:
      AssertionError: If report_errors is True and we found errors.
    Returns:
      A pytd.TypeDeclUnit
    """
    self.options.tweak(pythonpath=pythonpath,
                       module_name=module_name,
                       imports_map=imports_map,
                       quick=quick)
    errorlog = self._InitErrorLog(src)
    unit = infer.infer_types(src, errorlog, self.options, **kwargs)
    unit = pytd_utils.CanonicalOrdering(unit.Visit(visitors.VerifyVisitor()))
    if report_errors and errorlog.has_error():
      errorlog.print_to_stderr()
      self.fail("Inferencer found %d errors" % len(errorlog))
    return unit
Beispiel #3
0
 def InferFromFile(self, filename, pythonpath):
   self.options.tweak(pythonpath=pythonpath)
   with open(filename, "rb") as fi:
     unit = infer.infer_types(fi.read(), self.errorlog, self.options,
                              filename=filename, cache_unknowns=True)
     unit.Visit(visitors.VerifyVisitor())
     return pytd_utils.CanonicalOrdering(unit)
Beispiel #4
0
  def _InferAndVerify(self, src, pythonpath=(), module_name=None,
                      imports_map=None, report_errors=False, quick=False,
                      **kwargs):
    """Infer types for the source code treating it as a module.

    Used by Infer().

    Args:
      src: The source code of a module. Treat it as "__main__".
      pythonpath: --pythonpath as list/tuple of string
      module_name: Name of the module we're analyzing. E.g. "foo.bar.mymodule".
      imports_map: --imports_info data
      report_errors: Whether to fail if the type inferencer reports any errors
        in the program.
      quick: Try to run faster, by avoiding costly computations.
      **kwargs: Keyword paramters to pass through to the type inferencer.

    Raises:
      AssertionError: If report_errors is True and we found errors.
    Returns:
      A pytd.TypeDeclUnit
    """
    self.options.tweak(pythonpath=pythonpath,
                       module_name=module_name,
                       imports_map=imports_map,
                       quick=quick)
    errorlog = self._InitErrorLog(src)
    unit = infer.infer_types(src, errorlog, self.options, **kwargs)
    unit = pytd_utils.CanonicalOrdering(unit.Visit(visitors.VerifyVisitor()))
    if report_errors and errorlog.has_error():
      errorlog.print_to_stderr()
      self.fail("Inferencer found %d errors" % len(errorlog))
    return unit
Beispiel #5
0
 def InferAndCheck(self, code):
   errorlog = errors.ErrorLog()
   unit = infer.infer_types(
       textwrap.dedent(code), self.PYTHON_VERSION, errorlog, deep=True,
       solve_unknowns=True, reverse_operators=True, cache_unknowns=True)
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit), errorlog
Beispiel #6
0
  def _InferAndVerify(self, src, pythonpath=(), imports_map=None,
                      report_errors=False, **kwargs):
    """Infer types for the source code treating it as a module.

    Used by class Infer (which sets up a 'with' framework)

    Args:
      src: The source code of a module. Treat it as "__main__".
      pythonpath: --pythonpath as list/tuple of string
      imports_map: --imports_info data
      report_errors: Whether to fail if the type inferencer reports any errors
        in the program.
      **kwargs: Keyword paramters to pass through to the type inferencer.

    Raises:
      AssertionError: If report_errors is True and we found errors.
    Returns:
      A pytd.TypeDeclUnit
    """
    self.options.tweak(pythonpath=pythonpath, imports_map=imports_map)
    errorlog = self._InitErrorLog(src)
    unit = infer.infer_types(src, errorlog, self.options, **kwargs)
    unit = pytd_utils.CanonicalOrdering(unit.Visit(visitors.VerifyVisitor()))
    if report_errors and errorlog.has_error():
      errorlog.print_to_stderr()
      self.fail("Inferencer found %d errors" % len(errorlog))
    return unit
Beispiel #7
0
 def InferAndCheck(self, code):
   unit = infer.infer_types(
       textwrap.dedent(code), self.errorlog, self.options,
       deep=True,
       reverse_operators=True, cache_unknowns=True)
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit), self.errorlog
Beispiel #8
0
 def InferAndCheck(self, code, deep=True, pythonpath=(), **kwargs):
   self.options.tweak(pythonpath=pythonpath)
   code = textwrap.dedent(code)
   errorlog = self._InitErrorLog(code)
   unit = infer.infer_types(
       code, errorlog, self.options, deep=deep, cache_unknowns=True, **kwargs)
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit), errorlog
Beispiel #9
0
 def InferAndCheck(self, code, deep=True, pythonpath=()):
   self.options.tweak(pythonpath=pythonpath)
   code = textwrap.dedent(code)
   errorlog = self._InitErrorLog(code)
   unit = infer.infer_types(
       code, errorlog, self.options, deep=deep, cache_unknowns=True)
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit), errorlog
Beispiel #10
0
 def InferFromFile(self, filename, pythonpath, find_pytd_import_ext=".pytd"):
   with open(filename, "rb") as fi:
     unit = infer.infer_types(fi.read(), self.PYTHON_VERSION,
                              filename=filename, cache_unknowns=True,
                              pythonpath=pythonpath,
                              find_pytd_import_ext=find_pytd_import_ext)
     unit.Visit(visitors.VerifyVisitor())
     return pytd_utils.CanonicalOrdering(unit)
Beispiel #11
0
 def InferFromFile(self, filename, pythonpath):
   self.options.tweak(pythonpath=pythonpath)
   with open(filename, "rb") as fi:
     code = fi.read()
     errorlog = self._InitErrorLog(code, filename)
     unit = infer.infer_types(code, errorlog, self.options,
                              filename=filename, cache_unknowns=True)
     unit.Visit(visitors.VerifyVisitor())
     return pytd_utils.CanonicalOrdering(unit)
Beispiel #12
0
 def InferFromFile(self, filename, pythonpath, find_pytd_import_ext=".pytd"):
   errorlog = errors.ErrorLog()
   with open(filename, "rb") as fi:
     unit = infer.infer_types(fi.read(), self.PYTHON_VERSION, errorlog,
                              filename=filename, cache_unknowns=True,
                              pythonpath=pythonpath,
                              find_pytd_import_ext=find_pytd_import_ext)
     unit.Visit(visitors.VerifyVisitor())
     return pytd_utils.CanonicalOrdering(unit)
Beispiel #13
0
 def assert_ok(self, code, raises=None,
               pythonpath=(), find_pytd_import_ext=".pytd"):
   """Run an inference smoke test for the given code."""
   if raises is not None:
     # TODO(kramm): support this
     log.warning("Ignoring 'raises' parameter to assert_ok")
   unit = infer.infer_types(
       textwrap.dedent(code), self.PYTHON_VERSION,
       deep=False, solve_unknowns=False, reverse_operators=True,
       pythonpath=pythonpath, find_pytd_import_ext=find_pytd_import_ext,
       cache_unknowns=True)
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit)
Beispiel #14
0
 def InferFromFile(self, filename, pythonpath):
     self.options.tweak(pythonpath=pythonpath)
     with open(filename, "rb") as fi:
         code = fi.read()
         errorlog = errors.ErrorLog()
         loader = load_pytd.Loader(
             infer.get_module_name(filename, self.options), self.options)
         unit, _ = infer.infer_types(code,
                                     errorlog,
                                     self.options,
                                     loader=loader,
                                     filename=filename,
                                     cache_unknowns=True)
         unit.Visit(visitors.VerifyVisitor())
         return pytd_utils.CanonicalOrdering(unit)
Beispiel #15
0
  def _InferAndVerify(self, src, **kwargs):
    """Infer types for the source code treating it as a module.

    Used by class Infer (which sets up a 'with' framework)

    Args:
      src: The source code of a module. Treat it as "__main__".
      **kwargs: Keyword paramters to pass through to the type inferencer.

    Returns:
      A pytd.TypeDeclUnit
    """
    unit = infer.infer_types(src, self.PYTHON_VERSION, **kwargs)
    unit.Visit(visitors.VerifyVisitor())
    return pytd_utils.CanonicalOrdering(unit)
Beispiel #16
0
 def assertNoErrors(self, code, raises=None,
                    pythonpath=(),
                    report_errors=True):
   """Run an inference smoke test for the given code."""
   if raises is not None:
     # TODO(kramm): support this
     log.warning("Ignoring 'raises' parameter to assertNoErrors")
   self.options.tweak(pythonpath=pythonpath)
   errorlog = self._InitErrorLog(code)
   unit = infer.infer_types(
       textwrap.dedent(code), errorlog, self.options,
       deep=True, solve_unknowns=True, cache_unknowns=True)
   if report_errors and errorlog.has_error():
     errorlog.print_to_stderr()
     self.fail("Inferencer found %d errors" % len(errorlog))
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit)
Beispiel #17
0
 def assertNoErrors(self, code, raises=None,
                    pythonpath=(),
                    report_errors=True):
   """Run an inference smoke test for the given code."""
   if raises is not None:
     # TODO(kramm): support this
     log.warning("Ignoring 'raises' parameter to assertNoErrors")
   self.options.tweak(pythonpath=pythonpath)
   unit = infer.infer_types(
       textwrap.dedent(code), self.errorlog, self.options,
       deep=False, solve_unknowns=False, reverse_operators=True,
       cache_unknowns=True)
   if report_errors and self.errorlog.errors:
     self.errorlog.print_to_stderr()
     self.fail("Inferencer found %d errors" % len(self.errorlog))
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit)
Beispiel #18
0
 def assertNoErrors(self, code, raises=None,
                    pythonpath=(), find_pytd_import_ext=".pytd",
                    report_errors=True):
   """Run an inference smoke test for the given code."""
   if raises is not None:
     # TODO(kramm): support this
     log.warning("Ignoring 'raises' parameter to assertNoErrors")
   errorlog = errors.ErrorLog()
   unit = infer.infer_types(
       textwrap.dedent(code), self.PYTHON_VERSION, errorlog,
       deep=False, solve_unknowns=False, reverse_operators=True,
       pythonpath=pythonpath, find_pytd_import_ext=find_pytd_import_ext,
       cache_unknowns=True)
   if report_errors and errorlog.errors:
     errorlog.print_to_stderr()
     self.fail("Inferencer found %d errors" % len(errorlog.errors))
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit)
Beispiel #19
0
 def assertNoErrors(self, code, raises=None,
                    pythonpath=(), find_pytd_import_ext=".pytd",
                    report_errors=True):
   """Run an inference smoke test for the given code."""
   if raises is not None:
     # TODO(kramm): support this
     log.warning("Ignoring 'raises' parameter to assertNoErrors")
   errorlog = errors.ErrorLog()
   unit = infer.infer_types(
       textwrap.dedent(code), self.PYTHON_VERSION, errorlog,
       deep=False, solve_unknowns=False, reverse_operators=True,
       pythonpath=pythonpath, find_pytd_import_ext=find_pytd_import_ext,
       cache_unknowns=True)
   if report_errors and errorlog.errors:
     errorlog.print_to_stderr()
     self.fail("Inferencer found %d errors" % len(errorlog))
   unit.Visit(visitors.VerifyVisitor())
   return pytd_utils.CanonicalOrdering(unit)
Beispiel #20
0
 def InferAndCheck(self, code, deep=True, pythonpath=(), **kwargs):
     self.options.tweak(pythonpath=pythonpath)
     code = textwrap.dedent(code)
     errorlog = errors.ErrorLog()
     unit, builtins_pytd = infer.infer_types(code,
                                             errorlog,
                                             self.options,
                                             deep=deep,
                                             analyze_annotated=True,
                                             cache_unknowns=True,
                                             **kwargs)
     unit.Visit(visitors.VerifyVisitor())
     unit = optimize.Optimize(unit,
                              builtins_pytd,
                              lossy=False,
                              use_abcs=False,
                              max_union=7,
                              remove_mutable=False)
     return pytd_utils.CanonicalOrdering(unit), errorlog
Beispiel #21
0
  def _InferAndVerify(self, src, report_errors=False, **kwargs):
    """Infer types for the source code treating it as a module.

    Used by class Infer (which sets up a 'with' framework)

    Args:
      src: The source code of a module. Treat it as "__main__".
      report_errors: Whether to fail if the type inferencer reports any errors
        in the program.
      **kwargs: Keyword paramters to pass through to the type inferencer.

    Raises:
      AssertionError: If report_errors is True and we found errors.
    Returns:
      A pytd.TypeDeclUnit
    """
    errorlog = errors.ErrorLog()
    unit = infer.infer_types(src, self.PYTHON_VERSION, errorlog, **kwargs)
    unit = pytd_utils.CanonicalOrdering(unit.Visit(visitors.VerifyVisitor()))
    if report_errors and errorlog.errors:
      errorlog.print_to_stderr()
      self.fail("Inferencer found %d errors" % len(errorlog.errors))
    return unit
Beispiel #22
0
  def _InferAndVerify(self, src, report_errors=False, **kwargs):
    """Infer types for the source code treating it as a module.

    Used by class Infer (which sets up a 'with' framework)

    Args:
      src: The source code of a module. Treat it as "__main__".
      report_errors: Whether to fail if the type inferencer reports any errors
        in the program.
      **kwargs: Keyword paramters to pass through to the type inferencer.

    Raises:
      AssertionError: If report_errors is True and we found errors.
    Returns:
      A pytd.TypeDeclUnit
    """
    errorlog = errors.ErrorLog()
    unit = infer.infer_types(src, self.PYTHON_VERSION, errorlog, **kwargs)
    unit = pytd_utils.CanonicalOrdering(unit.Visit(visitors.VerifyVisitor()))
    if report_errors and errorlog:
      errorlog.print_to_stderr()
      self.fail("Inferencer found %d errors" % len(errorlog))
    return unit