def ParseWithBuiltins(self, src): ast = parser.TypeDeclParser().Parse(textwrap.dedent(src)) b, t = builtins.GetBuiltinsAndTyping() ast = ast.Visit(visitors.LookupExternalTypes( {"__builtin__": b, "typing": t}, full_names=True)) ast = ast.Visit(visitors.NamedTypeToClassType()) ast = visitors.AdjustTypeParameters(ast) ast.Visit(visitors.FillInModuleClasses({"": ast})) ast.Visit(visitors.VerifyVisitor()) return ast
def Parse(self, src, version=None, platform=None): # TODO(kramm): Using self.parser here breaks tests. Why? tree = parser.TypeDeclParser(version=version, platform=platform).Parse( textwrap.dedent(src)) tree = tree.Visit(visitors.NamedTypeToClassType()) tree = visitors.AdjustTypeParameters(tree) # Convert back to named types for easier testing tree = tree.Visit(visitors.ClassTypeToNamedType()) tree.Visit(visitors.VerifyVisitor()) return tree
def ToAST(self, src_or_tree): # TODO(pludemann): The callers are not consistent in how they use this # and in most (all?) cases they know whether they're # passing in a source string or parse tree. It would # be better if all the calles were consistent. if isinstance(src_or_tree, basestring): # Put into a canonical form (removes comments, standard indents): return self.Parse(src_or_tree + "\n") else: # isinstance(src_or_tree, tuple): src_or_tree.Visit(visitors.VerifyVisitor()) return src_or_tree
def InferFromFile(self, filename, pythonpath): self.options.tweak(pythonpath=pythonpath) with open(filename, "rb") as fi: code = fi.read() errorlog = errors.ErrorLog() unit, _ = infer.infer_types(code, errorlog, self.options, filename=filename, cache_unknowns=True) unit.Visit(visitors.VerifyVisitor()) return pytd_utils.CanonicalOrdering(unit)
def InferWithErrors(self, code, deep=True, pythonpath=(), **kwargs): kwargs.update(self._SetUpErrorHandling(code, pythonpath)) unit, builtins_pytd = analyze.infer_types(deep=deep, analyze_annotated=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), kwargs["errorlog"]
def Parse(self, src, name=None, version=None, platform=None): version = version or self.PYTHON_VERSION # TODO(kramm): Using self.parser here breaks tests. Why? tree = parser.parse_string(textwrap.dedent(src), name=name, python_version=version, platform=platform) tree = tree.Visit(visitors.NamedTypeToClassType()) tree = tree.Visit(visitors.AdjustTypeParameters()) # Convert back to named types for easier testing tree = tree.Visit(visitors.ClassTypeToNamedType()) tree.Visit(visitors.VerifyVisitor()) return tree
def ParseWithBuiltins(self, src): ast = parser.parse_string(textwrap.dedent(src)) b, t = builtins.GetBuiltinsAndTyping() ast = ast.Visit( visitors.LookupExternalTypes({ "__builtin__": b, "typing": t }, full_names=True)) ast = ast.Visit(visitors.NamedTypeToClassType()) ast = ast.Visit(visitors.AdjustTypeParameters()) ast.Visit(visitors.FillInLocalPointers({"": ast, "__builtin__": b})) ast.Visit(visitors.VerifyVisitor()) return ast
def InferFromFile(self, filename, pythonpath): with open(filename, "rb") as fi: code = fi.read() errorlog = errors.ErrorLog() self.loader = load_pytd.Loader(analyze.get_module_name( filename, pythonpath), self.PYTHON_VERSION, pythonpath=pythonpath) unit, _ = analyze.infer_types(code, errorlog, self.options, loader=self.loader, filename=filename) unit.Visit(visitors.VerifyVisitor()) return pytd_utils.CanonicalOrdering(unit)
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
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)
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)
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
def ParseWithBuiltins(self, src): ast = parser.parse_string(textwrap.dedent(src), python_version=self.PYTHON_VERSION) ast = ast.Visit( visitors.LookupExternalTypes( { "__builtin__": self.loader.builtins, "typing": self.loader.typing }, full_names=True)) ast = ast.Visit(visitors.NamedTypeToClassType()) ast = ast.Visit(visitors.AdjustTypeParameters()) ast.Visit( visitors.FillInLocalPointers({ "": ast, "__builtin__": self.loader.builtins })) ast.Visit(visitors.VerifyVisitor()) return ast
def testDefaceUnresolved(self): builtins = self.Parse(textwrap.dedent(""" class int(object): pass """)) src = textwrap.dedent(""" class A(X): def a(self, a: A, b: X, c: int) -> X raises X def b(self) -> X[int] """) expected = textwrap.dedent(""" class A(?): def a(self, a: A, b: ?, c: int) -> ? raises ? def b(self) -> ? """) tree = self.Parse(src) new_tree = tree.Visit(visitors.DefaceUnresolved([tree, builtins])) new_tree.Visit(visitors.VerifyVisitor()) self.AssertSourceEquals(new_tree, expected)
def testDefaceUnresolved2(self): builtins = self.Parse(textwrap.dedent(""" class int(object): pass T = TypeVar("T") class list(Generic[T]): pass """)) src = textwrap.dedent(""" class A(X): def a(self, a: A, b: X, c: int) -> X raises X def c(self) -> Union[list[X], int] """) expected = textwrap.dedent(""" class A(?): def a(self, a: A, b: ?, c: int) -> ? raises ? def c(self) -> Union[list[?], int] """) tree = self.Parse(src) new_tree = tree.Visit(visitors.DefaceUnresolved([tree, builtins])) new_tree.Visit(visitors.VerifyVisitor()) self.AssertSourceEquals(new_tree, expected)
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
def canonical_pyi(pyi): ast = parser.TypeDeclParser().Parse(pyi) ast = ast.Visit(visitors.ClassTypeToNamedType()) ast = ast.Visit(visitors.CanonicalOrderingVisitor(sort_signatures=True)) ast.Visit(visitors.VerifyVisitor()) return pytd.Print(ast)
def canonical_pyi(pyi, python_version): ast = parser.parse_string(pyi, python_version=python_version) ast = ast.Visit(visitors.ClassTypeToNamedType()) ast = ast.Visit(visitors.CanonicalOrderingVisitor(sort_signatures=True)) ast.Visit(visitors.VerifyVisitor()) return pytd.Print(ast)
def Parse(self, src, version=None): # TODO(kramm): Using self.parser here breaks tests. Why? tree = parser.TypeDeclParser(version=version).Parse(textwrap.dedent(src)) tree.Visit(visitors.VerifyVisitor()) return tree