Example #1
0
def test_simple_polymorphic_call():
    tc.reset()
    tv1 = tc.fresh_tvar()
    tv2 = tc.fresh_tvar()
    fn1 = create_Callable([tv1, tv2], bool, [tv1, tv2])
    fn2 = create_Callable([int, int], bool)

    unify_helper(fn1, fn2, Callable[[int, int], bool])
Example #2
0
def test_simple_polymorphic_call():
    tc.reset()
    tv1 = tc.fresh_tvar()
    tv2 = tc.fresh_tvar()
    fn1 = create_Callable([tv1, tv2], bool, {tv1, tv2})
    fn2 = create_Callable([int, int], bool)

    unify_helper(fn1, fn2, Callable[[int, int], bool])
Example #3
0
def test_higher_order_polymorphic_call():
    tc.reset()
    tv1 = tc.fresh_tvar()
    tv2 = tc.fresh_tvar()

    fn0 = create_Callable([tv1, int], int, [tv1])
    fn1 = create_Callable([int, int], int)

    fn2 = create_Callable([fn0, int], bool)
    fn3 = create_Callable([fn1, int], bool)
    fn4 = create_Callable([tv2, int], bool, [tv2])

    unify_helper(fn2, fn3, Callable[[Callable[[int, int], int], int], bool])
    unify_helper(fn2, fn4, Callable[[Callable[[int, int], int], int], bool])

    resolve_helper(tv1, int)
    resolve_helper(tv2, Callable[[int, int], int])
Example #4
0
def test_higher_order_polymorphic_call():
    tc.reset()
    tv1 = tc.fresh_tvar()
    tv2 = tc.fresh_tvar()

    fn0 = create_Callable([tv1, int], int, {tv1})
    fn1 = create_Callable([int, int], int)

    fn2 = create_Callable([fn0, int], bool)
    fn3 = create_Callable([fn1, int], bool)
    fn4 = create_Callable([tv2, int], bool, {tv2})

    unify_helper(fn2, fn3, Callable[[Callable[[int, int], int], int], bool])
    unify_helper(fn2, fn4, Callable[[Callable[[int, int], int], int], bool])

    resolve_helper(tv1, int)
    resolve_helper(tv2, Callable[[int, int], int])
Example #5
0
    def __init__(self, type_constraints):
        self.type_constraints = type_constraints
        with open(TYPE_SHED_PATH) as f:
            contents = '\n'.join(f.readlines())
        module = astroid.parse(contents)
        self.classes = defaultdict(dict)
        self.functions = defaultdict(list)
        for class_def in module.nodes_of_class(astroid.ClassDef):
            for base in class_def.bases:
                if isinstance(base, astroid.Subscript):
                    gen = base.value.as_string()
                    tvars = base.slice.as_string().strip('()').split(',')
                    if gen == 'Generic':
                        self.classes[class_def.name]['__pyta_tvars'] = tvars
            for function_def in class_def.nodes_of_class(astroid.FunctionDef):
                arg_types = []
                tvars = self.classes[class_def.name].get('__pyta_tvars', [])
                poly_tvars = [(eval(tvar, globals())) for tvar in tvars]
                for annotation in function_def.args.annotations:
                    if annotation is None:
                        # assume this is the first parameter 'self'
                        assert arg_types == []
                        arg_types.append(
                            eval(self._builtin_to_typing(class_def.name),
                                 globals()))
                    else:
                        arg_types.append(
                            eval(
                                self._builtin_to_typing(
                                    annotation.as_string()), globals()))

                rtype = eval(
                    self._builtin_to_typing(function_def.returns.as_string()),
                    globals())

                self.classes[class_def.name][function_def.name] = (
                    create_Callable(arg_types, rtype,
                                    poly_vars=poly_tvars), class_def.name)
                self.functions[function_def.name].append(
                    create_Callable(arg_types, rtype, poly_vars=poly_tvars))