Exemple #1
0
 def testRemoveInheritedMethodsWithOverride(self):
     src = textwrap.dedent("""
     class A(object):
         def f(self, x) -> ?
     class B(A):
         def f(self) -> ?
     class C(B):
         def f(self) -> ?
     class D(B):
         def f(self, x) -> ?
 """)
     expected = textwrap.dedent("""
     class A(object):
         def f(self, x) -> ?
     class B(A):
         def f(self) -> ?
     class C(B):
         pass
     class D(B):
         def f(self, x) -> ?
 """)
     ast = self.Parse(src)
     ast = visitors.LookupClasses(ast, self.builtins)
     ast = ast.Visit(optimize.RemoveInheritedMethods())
     self.AssertSourceEquals(ast, expected)
Exemple #2
0
 def testRemoveInheritedMethodsWithCircle(self):
     src = textwrap.dedent("""
     class A(B):
         def f(self) -> ?
     class B(A):
         def f(self) -> ?
 """)
     ast = self.Parse(src)
     ast = visitors.LookupClasses(ast, self.builtins)
     ast = ast.Visit(optimize.RemoveInheritedMethods())
     self.AssertSourceEquals(ast, src)
Exemple #3
0
 def testDontRemoveAbstractMethodImplementation(self):
     src = textwrap.dedent("""
   class A(object):
     @abstractmethod
     def foo(self): ...
   class B(A):
     def foo(self): ...
 """)
     ast = self.Parse(src)
     ast = visitors.LookupClasses(ast, self.builtins)
     ast = ast.Visit(optimize.RemoveInheritedMethods())
     self.AssertSourceEquals(ast, src)
Exemple #4
0
 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)
Exemple #5
0
 def testRemoveInheritedMethodsWithDiamond(self):
     src = textwrap.dedent("""
     class A(object):
         def f(self, x) -> Any
     class B(A):
         pass
     class C(A):
         def f(self, x, y) -> Any
     class D(B, C):
         def f(self, x) -> Any
 """)
     ast = self.Parse(src)
     ast = visitors.LookupClasses(ast, builtins.GetBuiltinsPyTD())
     ast = ast.Visit(optimize.RemoveInheritedMethods())
     self.AssertSourceEquals(ast, src)
Exemple #6
0
 def test_remove_inherited_methods_with_diamond(self):
     src = textwrap.dedent("""
     class A(object):
         def f(self, x) -> ?
     class B(A):
         pass
     class C(A):
         def f(self, x, y) -> ?
     class D(B, C):
         def f(self, x) -> ?
 """)
     ast = self.Parse(src)
     ast = visitors.LookupClasses(ast, self.builtins)
     ast = ast.Visit(optimize.RemoveInheritedMethods())
     self.AssertSourceEquals(ast, src)
Exemple #7
0
    def testRemoveInheritedMethodsWithoutSelf(self):
        src = textwrap.dedent("""
        class Bar(object):
          def baz(self) -> int

        class Foo(Bar):
          def baz(self) -> int
          def bar() -> float
    """)
        expected = textwrap.dedent("""
        class Bar(object):
          def baz(self) -> int

        class Foo(Bar):
          def bar() -> float
    """)
        ast = self.Parse(src)
        ast = visitors.LookupClasses(ast, self.builtins)
        ast = ast.Visit(optimize.RemoveInheritedMethods())
        self.AssertSourceEquals(ast, expected)
Exemple #8
0
    def testRemoveInheritedMethods(self):
        src = textwrap.dedent("""
        class A():
            def f(self, y: int) -> bool
            def g(self) -> float

        class B(A):
            def b(self) -> B
            def f(self, y: int) -> bool

        class C(A):
            def c(self) -> C
            def f(self, y: int) -> bool

        class D(B):
            def g(self) -> float
            def d(self) -> D
    """)
        expected = textwrap.dedent("""
        class A():
            def f(self, y: int) -> bool
            def g(self) -> float

        class B(A):
            def b(self) -> B

        class C(A):
            def c(self) -> C

        class D(B):
            def d(self) -> D
    """)
        ast = self.Parse(src)
        ast = visitors.LookupClasses(
            ast, builtins.GetBuiltinsPyTD(self.PYTHON_VERSION))
        ast = ast.Visit(optimize.RemoveInheritedMethods())
        self.AssertSourceEquals(ast, expected)