def check_exprs(src, env=None, errors=None): node = parse(src) env = env or {} env.update(def_types(node)) node = check(node, Environ(env, errors)) LocationChecker().visit(node) return node
def testModule(self): foo_node = parse(""" def func1 div ./func2 def func2 div bar/func3 """) bar_node = parse(""" def func3 div "Text" """) foo_node = NamesResolver('foo').visit(foo_node) bar_node = NamesResolver('bar').visit(bar_node) node = collect_defs([foo_node, bar_node]) env = Environ(def_types(node)) node = check(node, env) modules = split_defs(node) foo_node = modules['foo'] bar_node = modules['bar'] foo_node = NamesUnResolver('foo').visit(foo_node) bar_node = NamesUnResolver('bar').visit(bar_node) foo_module = compile_module(foo_node) _exec_in(compile(foo_module, '<kinko:foo>', 'exec'), {}) self.compareSources( dumps(foo_module), """ def func1(ctx): ctx.buffer.write('<div>') func2(ctx) ctx.buffer.write('</div>') def func2(ctx): ctx.buffer.write('<div>') ctx.lookup('bar/func3')(ctx) ctx.buffer.write('</div>') """, ) bar_module = compile_module(bar_node) _exec_in(compile(bar_module, '<kinko:bar>', 'exec'), {}) self.compareSources( dumps(bar_module), """ def func3(ctx): ctx.buffer.write('<div>Text</div>') """, )
def test_dependent(): node = parse(""" def foo ./bar :arg "value" def bar #arg """) node = NamesResolver('test').visit(node) env = Environ(def_types(node)) node = check(node, env) foo_expr, bar_expr = node.values check_eq(foo_expr.__type__, Func[[], StringType]) check_eq(bar_expr.__type__, Func[[NamedArg['arg', TypeVar[None]]], TypeVar[None]]) # checks that TypeVar instance is the same assert bar_expr.__type__.__instance__.__args__[0].__arg_type__ is \ bar_expr.__type__.__instance__.__result__