def testGetBasesInMRO(self): ast = parser.parse_string(textwrap.dedent(""" from typing import Generic, TypeVar T = TypeVar("T") class Foo(Generic[T]): pass class Bar(Foo[int]): pass """), python_version=self.PYTHON_VERSION) ast = ast.Visit(visitors.AdjustTypeParameters()) loader = load_pytd.Loader(None, self.PYTHON_VERSION) ast = loader.resolve_ast(ast) bases = mro.GetBasesInMRO(ast.Lookup("Bar"), lookup_ast=ast) self.assertListEqual(["Foo", "typing.Generic", "__builtin__.object"], [t.name for t in bases])
def test_get_bases_in_mro(self): ast = parser.parse_string(textwrap.dedent(""" from typing import Generic, TypeVar T = TypeVar("T") class Foo(Generic[T]): pass class Bar(Foo[int]): pass """), options=self.options) ast = ast.Visit(visitors.AdjustTypeParameters()) loader = load_pytd.Loader( config.Options.create(python_version=self.python_version)) ast = loader.resolve_ast(ast) bases = mro.GetBasesInMRO(ast.Lookup("Bar"), lookup_ast=ast) self.assertListEqual(["Foo", "typing.Generic", "builtins.object"], [t.name for t in bases])
def _MaybeRemoveSignature(self, name, sig, is_abstract): """Visit a Signature and return None if we can remove it.""" if (not sig.params or sig.params[0].name != "self" or not isinstance(sig.params[0].type, pytd.ClassType)): return sig # Not a method cls = sig.params[0].type.cls if cls is None: # TODO(kramm): Remove once pytype stops generating ClassType(name, None). return sig try: if self._FindNameAndSig( mro.GetBasesInMRO(cls), name, (sig.Replace(params=sig.params[1:]), is_abstract)): return None # remove (see VisitFunction) except mro.MROError: return sig return sig
def testGetBasesInMRO(self): ast = parser.parse_string(textwrap.dedent(""" from typing import Generic, TypeVar T = TypeVar("T") class Foo(Generic[T]): pass class Bar(Foo[int]): pass """), python_version=self.PYTHON_VERSION) b, t = builtins.GetBuiltinsAndTyping(self.PYTHON_VERSION) ast = ast.Visit( visitors.LookupExternalTypes({ "__builtin__": b, "typing": t }, full_names=True)) ast = ast.Visit(visitors.NamedTypeToClassType()) bases = mro.GetBasesInMRO(ast.Lookup("Bar"), lookup_ast=ast) self.assertListEqual(["Foo", "typing.Generic", "__builtin__.object"], [t.name for t in bases])