def testUserSuperClassHierarchy(self): class_data = """ class AB: pass class EFG: pass class A(AB, EFG): pass class B(AB): pass class E(EFG, AB): pass class F(EFG): pass class G(EFG): pass """ src = """ def f(x: A or B, y: A, z: B) -> E or F or G def g(x: E or F or G or B) -> E or F def h(x) """ + class_data expected = """ def f(x: AB, y: A, z: B) -> EFG def g(x) -> EFG def h(x) """ + class_data hierarchy = self.Parse(src).Visit(visitors.ExtractSuperClasses()) visitor = optimize.FindCommonSuperClasses(hierarchy, use_abcs=False) new_src = self.ApplyVisitorToString(src, visitor) self.AssertSourceEquals(new_src, expected)
def testBuiltinSuperClasses(self): src = textwrap.dedent(""" def f(x: list or object, y: int or float) -> int or bool """) expected = textwrap.dedent(""" def f(x, y) -> int """) visitor = optimize.FindCommonSuperClasses(use_abcs=False) new_src = self.ApplyVisitorToString(src, visitor) self.AssertSourceEquals(new_src, expected)
def testSuperClasses(self): src = """ def f(x: list or tuple, y: frozenset or set) -> int or float def g(x: dict or Mapping, y: complex or int) -> set or dict or tuple or Container def h(x) """ expected = """ def f(x: Sequence, y: Set) -> Real def g(x: Mapping, y: Complex) -> Container def h(x) """ visitor = optimize.FindCommonSuperClasses(use_abcs=True) new_src = self.ApplyVisitorToString(src, visitor) self.AssertSourceEquals(new_src, expected)