示例#1
0
 def Check(self,
           code,
           pythonpath=(),
           skip_repeat_calls=True,
           report_errors=True,
           filename=None,
           quick=False,
           imports_map=None,
           **kwargs):
     """Run an inference smoke test for the given code."""
     self.ConfigureOptions(skip_repeat_calls=skip_repeat_calls,
                           quick=quick,
                           **self._GetPythonpathArgs(
                               pythonpath, imports_map))
     try:
         src = _Format(code)
         errorlog = test_utils.TestErrorLog(code)
         if errorlog.expected:
             self.fail(
                 "Cannot assert errors with Check(); use CheckWithErrors()")
         analyze.check_types(src,
                             filename,
                             loader=self.loader,
                             errorlog=errorlog,
                             options=self.options,
                             **kwargs)
     except directors.SkipFileError:
         pass
     if report_errors and errorlog:
         errorlog.print_to_stderr()
         self.fail("Checker found {} errors:\n{}".format(
             len(errorlog), errorlog))
示例#2
0
 def _SetUpErrorHandling(self, code, pythonpath, analyze_annotated, quick):
   code = _Format(code)
   errorlog = test_utils.TestErrorLog(code)
   self.ConfigureOptions(
       pythonpath=pythonpath, analyze_annotated=analyze_annotated, quick=quick)
   return {"src": code, "errorlog": errorlog, "options": self.options,
           "loader": self.loader}
示例#3
0
    def _InferAndVerify(self,
                        src,
                        pythonpath,
                        module_name,
                        report_errors,
                        analyze_annotated,
                        imports_map=None,
                        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".
      report_errors: Whether to fail if the type inferencer reports any errors
        in the program.
      analyze_annotated: Whether to analyze functions with return annotations.
      imports_map: --imports_info data
      quick: Try to run faster, by avoiding costly computations.
      **kwargs: Keyword parameters to pass through to the type inferencer.

    Raises:
      AssertionError: If report_errors is True and we found errors.
    Returns:
      A pytd.TypeDeclUnit
    """
        self.ConfigureOptions(module_name=module_name,
                              quick=quick,
                              use_pickled_files=True,
                              pythonpath=[""] if
                              (not pythonpath and imports_map) else pythonpath,
                              imports_map=imports_map,
                              analyze_annotated=analyze_annotated)
        errorlog = test_utils.TestErrorLog(src)
        if errorlog.expected:
            self.fail(
                "Cannot assert errors with Infer(); use InferWithErrors()")
        unit, builtins_pytd = analyze.infer_types(src,
                                                  errorlog,
                                                  self.options,
                                                  loader=self.loader,
                                                  **kwargs)
        unit.Visit(visitors.VerifyVisitor())
        if report_errors and errorlog:
            errorlog.print_to_stderr()
            self.fail("Inferencer found {} errors:\n{}".format(
                len(errorlog), errorlog))
        return unit, builtins_pytd
示例#4
0
 def InferFromFile(self, filename, pythonpath):
   with open(filename, "r") as fi:
     code = fi.read()
     errorlog = test_utils.TestErrorLog(code)
     if errorlog.expected:
       self.fail(
           "Cannot assert errors with InferFromFile(); use InferWithErrors()")
     self.ConfigureOptions(
         module_name=load_pytd.get_module_name(filename, pythonpath),
         pythonpath=pythonpath)
     unit, _ = analyze.infer_types(code, errorlog, self.options,
                                   loader=self.loader, filename=filename)
     unit.Visit(visitors.VerifyVisitor())
     return pytd_utils.CanonicalOrdering(unit)
示例#5
0
 def test_populate_marks(self):
     # Test that assert_error_regexes populates self.marks if not already done.
     errorlog = test_utils.TestErrorLog("x = 0")
     self.assertIsNone(errorlog.marks)
     self.assertErrorRegexes(errorlog, {})
     self.assertIsNotNone(errorlog.marks)