def test_strict(self): ast = parser.parse_string(pytd_src(""" import typing T = TypeVar('T') class list(typing.Generic[T], object): pass class A(): pass class B(A): pass class `~unknown0`(): pass a = ... # type: A def left() -> `~unknown0`: ... def right() -> list[A]: ... """), options=self.options) ast = self.LinkAgainstSimpleBuiltins(ast) m = type_match.TypeMatch(type_match.get_all_subclasses([ast])) left, right = ast.Lookup("left"), ast.Lookup("right") unknown0 = escape.unknown(0) self.assertEqual( m.match(left, right, {}), booleq.And((booleq.Eq(unknown0, "list"), booleq.Eq(f"{unknown0}.list.T", "A"))))
def _TestTypeParameters(self, reverse=False): ast = parser.parse_string(pytd_src(""" from typing import Any, Generic class `~unknown0`(): def next(self) -> Any: ... T = TypeVar('T') class A(Generic[T], object): def next(self) -> Any: ... class B(): pass def left(x: `~unknown0`) -> Any: ... def right(x: A[B]) -> Any: ... """), options=self.options) ast = self.LinkAgainstSimpleBuiltins(ast) m = type_match.TypeMatch() left, right = ast.Lookup("left"), ast.Lookup("right") match = m.match(right, left, {}) if reverse else m.match( left, right, {}) unknown0 = escape.unknown(0) self.assertEqual( match, booleq.And((booleq.Eq(unknown0, "A"), booleq.Eq(f"{unknown0}.A.T", "B")))) self.assertIn(f"{unknown0}.A.T", m.solver.variables)
def __init__(self, ctx): name = escape.unknown(Unknown._current_id) super().__init__(name, ctx) self.members = datatypes.MonitorDict() self.owner = None Unknown._current_id += 1 self.class_name = self.name self._calls = [] log.info("Creating %s", self.class_name)
def test_unknown_against_tuple(self): ast = self.ParseWithBuiltins(pytd_src(""" from typing import Tuple class `~unknown0`(): pass x = ... # type: Tuple[int, str] """)) unknown0 = escape.unknown(0) unk = ast.Lookup(unknown0) tup = ast.Lookup("x").type m = type_match.TypeMatch(type_match.get_all_subclasses([ast])) match = m.match_Unknown_against_Generic(unk, tup, {}) self.assertCountEqual(sorted(match.extract_equalities()), [(unknown0, "__builtin__.tuple"), (f"{unknown0}.__builtin__.tuple._T", "int"), (f"{unknown0}.__builtin__.tuple._T", "str")])