Esempio n. 1
0
 def _load_pickled_module(self, tempdir, module):
     pickle_loader = load_pytd.PickledPyiLoader(
         base_module=None,
         python_version=self.python_version,
         pythonpath=[tempdir.path])
     return pickle_loader.load_file(
         module.module_name, self._get_path(tempdir, module.file_name))
Esempio n. 2
0
 def testFunctionType(self):
     self.loader = load_pytd.PickledPyiLoader(
         base_module="bar",
         python_version=self.PYTHON_VERSION,
         pythonpath=[""])
     with utils.Tempdir() as d:
         pickled_foo = self.PicklePyi("""
     import UserDict
     def f(x: UserDict.UserDict) -> None: ...
   """,
                                      module_name="foo")
         foo = d.create_file("foo.pickled", pickled_foo)
         self.loader.imports_map = {"foo": foo}
         pickled_bar = self.PicklePyi("""
     from foo import f  # Alias(name="f", type=FunctionType("foo.f", f))
   """,
                                      module_name="bar")
         bar = d.create_file("bar.pickled", pickled_bar)
         self.assertNoCrash(self.Infer,
                            """
     from __future__ import google_type_annotations
     import bar
     bar.f(42)
   """,
                            imports_map={
                                "foo": foo,
                                "bar": bar
                            },
                            module_name="baz")
Esempio n. 3
0
 def _LoadPickledModule(self, tempdir, module):
     pickle_loader = load_pytd.PickledPyiLoader(
         base_module=None,
         python_version=self.PYTHON_VERSION,
         pythonpath=[tempdir.path])
     return pickle_loader.load_file(
         module.module_name, self._GetPath(tempdir, module.file_name))
Esempio n. 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 parameters 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(module_name=module_name, quick=quick)
        errorlog = errors.ErrorLog()
        self.loader = load_pytd.PickledPyiLoader(
            use_pickled_typeshed=False,
            base_module=module_name,
            python_version=self.PYTHON_VERSION,
            pythonpath=[""] if
            (not pythonpath and imports_map) else pythonpath,
            imports_map=imports_map)
        unit, builtins_pytd = analyze.infer_types(src,
                                                  errorlog,
                                                  self.options,
                                                  loader=self.loader,
                                                  **kwargs)
        unit.Visit(visitors.VerifyVisitor())
        unit = pytd_utils.CanonicalOrdering(unit)
        if report_errors and len(errorlog):
            errorlog.print_to_stderr()
            self.fail("Inferencer found %d errors" % len(errorlog))
        return unit, builtins_pytd
Esempio n. 5
0
 def _LoadPickledModule(self, tempdir, module):
   pickle_loader = load_pytd.PickledPyiLoader(
       base_module=None, options=self.options)
   return pickle_loader.load_file(
       module.module_name, self._GetPath(tempdir, module.file_name))
Esempio n. 6
0
 def _load_pickled_module(self, tempdir, module):
     pickle_loader = load_pytd.PickledPyiLoader(
         config.Options.create(python_version=self.python_version,
                               pythonpath=tempdir.path))
     return pickle_loader.load_file(
         module.module_name, self._get_path(tempdir, module.file_name))