def testRemoveRedundantSignature(self): src = textwrap.dedent(""" def foo(a: int) -> int def foo(a: int or bool) -> int """) expected = textwrap.dedent(""" def foo(a: int or bool) -> int """) ast = self.Parse(src) ast = ast.Visit(optimize.RemoveRedundantSignatures( optimize.SuperClassHierarchy({}))) self.AssertSourceEquals(ast, expected)
def test_remove_redundant_signature(self): src = pytd_src(""" def foo(a: int) -> int: ... def foo(a: Union[int, bool]) -> int: ... """) expected = pytd_src(""" def foo(a: Union[int, bool]) -> int: ... """) ast = self.Parse(src) ast = ast.Visit( optimize.RemoveRedundantSignatures(optimize.SuperClassHierarchy( {}))) self.AssertSourceEquals(ast, expected)
def testRemoveRedundantSignatureGenericLeftSide(self): src = textwrap.dedent(""" X = TypeVar("X") def foo(a: X, b: int) -> X def foo(a: X, b: Any) -> X """) expected = textwrap.dedent(""" X = TypeVar("X") def foo(a: X, b: Any) -> X """) ast = self.Parse(src) ast = ast.Visit(optimize.RemoveRedundantSignatures( optimize.SuperClassHierarchy({}))) self.AssertSourceEquals(ast, expected)
def test_remove_redundant_signature_polymorphic(self): src = textwrap.dedent(""" T = TypeVar("T") def foo(a: T) -> T def foo(a: int or bool) -> int """) expected = textwrap.dedent(""" T = TypeVar("T") def foo(a: T) -> T """) ast = self.Parse(src) ast = ast.Visit( optimize.RemoveRedundantSignatures(optimize.SuperClassHierarchy( {}))) self.AssertSourceEquals(ast, expected)
def test_remove_redundant_signature_with_exceptions(self): src = textwrap.dedent(""" def foo(a: int) -> int: raise IOError() def foo(a: int or bool) -> int """) expected = textwrap.dedent(""" def foo(a: int) -> int: raise IOError() def foo(a: int or bool) -> int """) ast = self.Parse(src) ast = ast.Visit( optimize.RemoveRedundantSignatures(optimize.SuperClassHierarchy( {}))) self.AssertSourceEquals(ast, expected)
def test_remove_redundant_signature_generic_left_side(self): src = pytd_src(""" from typing import Any X = TypeVar("X") def foo(a: X, b: int) -> X: ... def foo(a: X, b: Any) -> X: ... """) expected = pytd_src(""" from typing import Any X = TypeVar("X") def foo(a: X, b: Any) -> X: ... """) ast = self.Parse(src) ast = ast.Visit( optimize.RemoveRedundantSignatures(optimize.SuperClassHierarchy( {}))) self.AssertSourceEquals(ast, expected)
def testRemoveRedundantSignatureTemplate(self): src = textwrap.dedent(""" T = TypeVar("T") class A(Generic[T]): def foo(a: int) -> int def foo(a: T) -> T def foo(a: int or bool) -> int """) expected = textwrap.dedent(""" T = TypeVar("T") class A(Generic[T]): def foo(a: T) -> T def foo(a: int or bool) -> int """) ast = self.Parse(src) ast = ast.Visit(optimize.RemoveRedundantSignatures( optimize.SuperClassHierarchy({}))) self.AssertSourceEquals(ast, expected)
def test_remove_redundant_signature_template(self): src = pytd_src(""" T = TypeVar("T") class A(Generic[T]): def foo(a: int) -> int: ... def foo(a: T) -> T: ... def foo(a: Union[int, bool]) -> int: ... """) expected = pytd_src(""" T = TypeVar("T") class A(Generic[T]): def foo(a: T) -> T: ... def foo(a: Union[int, bool]) -> int: ... """) ast = self.Parse(src) ast = ast.Visit( optimize.RemoveRedundantSignatures(optimize.SuperClassHierarchy( {}))) self.AssertSourceEquals(ast, expected)
def testRemoveRedundantSignatureTwoTypeParams(self): src = textwrap.dedent(""" X = TypeVar("X") Y = TypeVar("Y") class A(Generic[X, Y]): def foo(a: X) -> Y def foo(a: Y) -> Y """) expected = textwrap.dedent(""" X = TypeVar("X") Y = TypeVar("Y") class A(Generic[X, Y]): def foo(a: X) -> Y def foo(a: Y) -> Y """) ast = self.Parse(src) ast = ast.Visit(optimize.RemoveRedundantSignatures( optimize.SuperClassHierarchy({}))) self.AssertSourceEquals(ast, expected)
def test_remove_redundant_signature_two_type_params(self): src = pytd_src(""" X = TypeVar("X") Y = TypeVar("Y") class A(Generic[X, Y]): def foo(a: X) -> Y: ... def foo(a: Y) -> Y: ... """) expected = pytd_src(""" X = TypeVar("X") Y = TypeVar("Y") class A(Generic[X, Y]): def foo(a: X) -> Y: ... def foo(a: Y) -> Y: ... """) ast = self.Parse(src) ast = ast.Visit( optimize.RemoveRedundantSignatures(optimize.SuperClassHierarchy( {}))) self.AssertSourceEquals(ast, expected)