Beispiel #1
0
    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)
Beispiel #2
0
 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: ...
   """))
Beispiel #3
0
 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())
Beispiel #4
0
  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])
Beispiel #5
0
    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])