print(2) func() defer.py This highlighted line (mis)uses "Variable annotations" proposed in PEP 52 6 . Variable annotations can accept arbitrary expression as the annotation. @defers decorater replaces the annotation line to a deferred func call. 5
as a: with bar() as b: with baz() as c: . .. @defers def func(): a = foo() defer: a.close() b = bar() defer: b.close() c = baz() defer: c.close() ... You can turn this ... ... into this
@conversationize def foo(): Alice: "Hi there" Bob: "Ayo, sup" Xzibit: "Yo dawg, I heard you like Python so I put Python in yo Python so you can eval while you eval" Alice says: Hi there Bob says: Ayo, sup Xzibit says: Yo dawg, I heard you like Python so I put Python in yo Python so you can eval while you eval
node): print(f'{node.target.id} says: {node.annotation.value}') def wrapper(): tree = ast.parse(inspect.getsource(f)) assert len(tree.body) == 1 and isinstance(tree.body[0], ast.FunctionDef), "should be a function" Transformer().visit(tree.body[0]) return wrapper @conversationize def foo(): Alice: "Hi there" 11 The implementation of the example decorator "conversationize"