def testOptional(self): ast = parser.parse_string(textwrap.dedent(""" def left(a: int) -> int def right(a: int, ...) -> int """)) m = type_match.TypeMatch() self.assertEquals(m.match(ast.Lookup("left"), ast.Lookup("right"), {}), booleq.TRUE)
def testOptional(self): ast = parser.parse_string( textwrap.dedent(""" def left(a: int) -> int def right(a: int, ...) -> int """)) m = type_match.TypeMatch() self.assertEquals(m.match(ast.Lookup("left"), ast.Lookup("right"), {}), booleq.TRUE)
def ParseBuiltinsFile(filename): """GetBuiltins(), but for a single file, not adding to builtins.modules. Only used in tests, e.g. for loading reduced or specialized builtin files. Args: filename: Filename, relative to pytypedecl/builtins/ Returns: A PyTypeDeclUnit for a single module. """ return parser.parse_string(_FindBuiltinFile(filename))
def testGeneric(self): ast = parser.parse_string(textwrap.dedent(""" class A<T extends nothing>(nothing): pass left: A<?> right: A<?> """)) ast = visitors.LookupClasses(ast) m = type_match.TypeMatch() self.assertEquals(m.match_type_against_type( ast.Lookup("left").type, ast.Lookup("right").type, {}), booleq.TRUE)
def testClassMatch(self): ast = parser.parse_string(textwrap.dedent(""" class Left(nothing): def method(self) -> ? class Right(nothing): def method(self) -> ? def method2(self) -> ? """)) ast = visitors.LookupClasses(ast) m = type_match.TypeMatch() left, right = ast.Lookup("Left"), ast.Lookup("Right") self.assertEquals(m.match(left, right, {}), booleq.TRUE) self.assertNotEquals(m.match(right, left, {}), booleq.TRUE)
def testGeneric(self): ast = parser.parse_string( textwrap.dedent(""" class A<T extends nothing>(nothing): pass left: A<?> right: A<?> """)) ast = visitors.LookupClasses(ast) m = type_match.TypeMatch() self.assertEquals( m.match_type_against_type( ast.Lookup("left").type, ast.Lookup("right").type, {}), booleq.TRUE)
def testClassMatch(self): ast = parser.parse_string( textwrap.dedent(""" class Left(nothing): def method(self) -> ? class Right(nothing): def method(self) -> ? def method2(self) -> ? """)) ast = visitors.LookupClasses(ast) m = type_match.TypeMatch() left, right = ast.Lookup("Left"), ast.Lookup("Right") self.assertEquals(m.match(left, right, {}), booleq.TRUE) self.assertNotEquals(m.match(right, left, {}), booleq.TRUE)
def testSubclasses(self): ast = parser.parse_string(textwrap.dedent(""" class A(nothing): pass class B(A): pass a : A def left(a: B) -> B def right(a: A) -> A """)) ast = visitors.LookupClasses(ast) m = type_match.TypeMatch({ast.Lookup("a").type: [ast.Lookup("B")]}) left, right = ast.Lookup("left"), ast.Lookup("right") self.assertEquals(m.match(left, right, {}), booleq.TRUE) self.assertNotEquals(m.match(right, left, {}), booleq.TRUE)
def testSubclasses(self): ast = parser.parse_string( textwrap.dedent(""" class A(nothing): pass class B(A): pass a : A def left(a: B) -> B def right(a: A) -> A """)) ast = visitors.LookupClasses(ast) m = type_match.TypeMatch({ast.Lookup("a").type: [ast.Lookup("B")]}) left, right = ast.Lookup("left"), ast.Lookup("right") self.assertEquals(m.match(left, right, {}), booleq.TRUE) self.assertNotEquals(m.match(right, left, {}), booleq.TRUE)