def testRemoveInheritedMethodsWithLateType(self): src = textwrap.dedent(""" class Foo(other.Bar): def bar() -> float """) expected = textwrap.dedent(""" class Foo(other.Bar): def bar() -> float """) ast = self.Parse(src) ast = ast.Visit( visitors.ReplaceTypes({"other.Bar": pytd.LateType("other.Bar")})) ast = ast.Visit(optimize.RemoveInheritedMethods()) ast = ast.Visit(visitors.LateTypeToClassType()) self.AssertSourceEquals(ast, expected)
def testFindCommonSuperClasses(self): src = textwrap.dedent(""" x = ... # type: int or other.Bar """) expected = textwrap.dedent(""" x = ... # type: int or other.Bar """) ast = self.Parse(src) ast = ast.Visit(visitors.ReplaceTypes( {"other.Bar": pytd.LateType("other.Bar")})) hierarchy = ast.Visit(visitors.ExtractSuperClassesByName()) ast = ast.Visit(optimize.FindCommonSuperClasses( optimize.SuperClassHierarchy(hierarchy))) ast = ast.Visit(visitors.LateTypeToClassType()) self.AssertSourceEquals(ast, expected)
def test_find_common_superclasses(self): src = pytd_src(""" x = ... # type: Union[int, other.Bar] """) expected = pytd_src(""" x = ... # type: Union[int, other.Bar] """) ast = self.Parse(src) ast = ast.Visit( visitors.ReplaceTypes({"other.Bar": pytd.LateType("other.Bar")})) hierarchy = ast.Visit(visitors.ExtractSuperClassesByName()) ast = ast.Visit( optimize.FindCommonSuperClasses( optimize.SuperClassHierarchy(hierarchy))) ast = ast.Visit(visitors.LateTypeToClassType()) self.AssertSourceEquals(ast, expected)
def write_pickle(ast, options, loader=None): """Dump a pickle of the ast to a file.""" loader = loader or load_pytd.create_loader(options) try: ast = serialize_ast.PrepareForExport(options.module_name, ast, loader) except parser.ParseError as e: if options.nofail: ast = serialize_ast.PrepareForExport( options.module_name, pytd_builtins.GetDefaultAst(options.python_version), loader) log.warning("***Caught exception: %s", str(e), exc_info=True) else: raise if options.verify_pickle: ast1 = ast.Visit(visitors.LateTypeToClassType()) ast1 = ast1.Visit(visitors.ClearClassPointers()) ast2 = loader.load_file(options.module_name, options.verify_pickle) ast2 = ast2.Visit(visitors.ClearClassPointers()) if not pytd_utils.ASTeq(ast1, ast2): raise AssertionError() serialize_ast.StoreAst(ast, options.output, options.open_function)