示例#1
0
 def InferFromFile(self, filename, pythonpath):
   with open(filename, "r") as fi:
     code = fi.read()
     errorlog = errors.ErrorLog()
     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)
示例#2
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)
示例#3
0
 def InferFromFile(self, filename, pythonpath, python_version=None):
     with open(filename, "rb") as fi:
         code = fi.read()
         errorlog = errors.ErrorLog()
         self.options.tweak(module_name=load_pytd.get_module_name(
             filename, pythonpath),
                            pythonpath=pythonpath,
                            python_version=python_version)
         self._CreateLoader()
         unit, _ = analyze.infer_types(code,
                                       errorlog,
                                       self.options,
                                       loader=self.loader,
                                       filename=filename)
         unit.Visit(visitors.VerifyVisitor())
         return pytd_utils.CanonicalOrdering(unit)
示例#4
0
 def test_filepath_to_module(self):
     # (filename, pythonpath, expected)
     test_cases = [("foo/bar/baz.py", [""], "foo.bar.baz"),
                   ("foo/bar/baz.py", ["foo"], "bar.baz"),
                   ("foo/bar/baz.py", ["fo"], "foo.bar.baz"),
                   ("foo/bar/baz.py", ["foo/"], "bar.baz"),
                   ("foo/bar/baz.py", ["foo", "bar"], "bar.baz"),
                   ("foo/bar/baz.py", ["foo/bar", "foo"], "baz"),
                   ("foo/bar/baz.py", ["foo", "foo/bar"], "bar.baz"),
                   ("./foo/bar.py", [""], "foo.bar"),
                   ("./foo.py", [""], "foo"), ("../foo.py", [""], None),
                   ("../foo.py", ["."], None),
                   ("foo/bar/../baz.py", [""], "foo.baz"),
                   ("../foo.py", [".."], "foo"),
                   ("../../foo.py", ["../.."], "foo"),
                   ("../../foo.py", [".."], None)]
     for filename, pythonpath, expected in test_cases:
         module = load_pytd.get_module_name(filename, pythonpath)
         self.assertEqual(module, expected)
示例#5
0
 def _store_module_name(self, module_name):
     if module_name is None:
         module_name = load_pytd.get_module_name(
             self.output_options.input, self.output_options.pythonpath)
     self.output_options.module_name = module_name