def testAddInheritedMethods(self): src = textwrap.dedent(""" class A(): foo = ... # type: bool def f(self, x: int) -> float def h(self) -> complex class B(A): bar = ... # type: int def g(self, y: int) -> bool def h(self, z: float) -> ? """) expected = textwrap.dedent(""" class A(): foo = ... # type: bool def f(self, x: int) -> float def h(self) -> complex class B(A): bar = ... # type: int foo = ... # type: bool def g(self, y: int) -> bool def h(self, z: float) -> ? def f(self, x: int) -> float """) ast = self.Parse(src) ast = visitors.LookupClasses(ast, builtins.GetBuiltinsPyTD()) ast = ast.Visit(optimize.AddInheritedMethods()) self.AssertSourceEquals(ast, expected)
def testAdjustInheritedMethodSelf(self): src = textwrap.dedent(""" class A(): def f(self: object) -> float class B(A): pass """) ast = self.Parse(src) ast = visitors.LookupClasses(ast, self.builtins) ast = ast.Visit(optimize.AddInheritedMethods()) self.assertMultiLineEqual(pytd.Print(ast.Lookup("B")), textwrap.dedent("""\ class B(A): def f(self) -> float: ... """))
def test_adjust_inherited_method_self(self): src = pytd_src(""" class A(): def f(self: object) -> float: ... class B(A): pass """) ast = self.Parse(src) ast = visitors.LookupClasses(ast, self.builtins) ast = ast.Visit(optimize.AddInheritedMethods()) self.assertMultiLineEqual( pytd_utils.Print(ast.Lookup("B")), pytd_src(""" class B(A): def f(self) -> float: ... """).lstrip())
def testAddInheritedMethods(self): src = textwrap.dedent(""" class A(): foo = ... # type: bool def f(self, x: int) -> float def h(self) -> complex class B(A): bar = ... # type: int def g(self, y: int) -> bool def h(self, z: float) -> ? """) ast = self.Parse(src) ast = visitors.LookupClasses(ast, builtins.GetBuiltinsPyTD()) self.assertItemsEqual(("g", "h"), [m.name for m in ast.Lookup("B").methods]) ast = ast.Visit(optimize.AddInheritedMethods()) self.assertItemsEqual(("f", "g", "h"), [m.name for m in ast.Lookup("B").methods])
def test_add_inherited_methods(self): src = pytd_src(""" from typing import Any class A(): foo = ... # type: bool def f(self, x: int) -> float: ... def h(self) -> complex: ... class B(A): bar = ... # type: int def g(self, y: int) -> bool: ... def h(self, z: float) -> Any: ... """) ast = self.Parse(src) ast = visitors.LookupClasses(ast, self.builtins) self.assertCountEqual(("g", "h"), [m.name for m in ast.Lookup("B").methods]) ast = ast.Visit(optimize.AddInheritedMethods()) self.assertCountEqual(("f", "g", "h"), [m.name for m in ast.Lookup("B").methods])