From f69ccd242129a6767c0ab5dcab981654702366d5 Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Sun, 31 Dec 2017 00:04:15 -0500 Subject: [PATCH] Allow coroutines to be decorated --- hy/compiler.py | 6 ++++-- tests/native_tests/py35_only_tests.hy | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index b3fdf5a..1a7c216 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -76,6 +76,9 @@ def _is_hy_builtin(name, module_name): _compile_table = {} +_decoratables = (ast.FunctionDef, ast.ClassDef) +if PY35: + _decoratables += (ast.AsyncFunctionDef,) def ast_str(foobar): @@ -1301,8 +1304,7 @@ class HyASTCompiler(object): def compile_decorate_expression(self, expr): expr.pop(0) # with-decorator fn = self.compile(expr.pop()) - if not fn.stmts or not isinstance(fn.stmts[-1], (ast.FunctionDef, - ast.ClassDef)): + if not fn.stmts or not isinstance(fn.stmts[-1], _decoratables): raise HyTypeError(expr, "Decorated a non-function") decorators, ret, _ = self._compile_collect(expr) fn.stmts[-1].decorator_list = decorators + fn.stmts[-1].decorator_list diff --git a/tests/native_tests/py35_only_tests.hy b/tests/native_tests/py35_only_tests.hy index 9081dac..1301b72 100644 --- a/tests/native_tests/py35_only_tests.hy +++ b/tests/native_tests/py35_only_tests.hy @@ -45,6 +45,16 @@ (assert (= (run-coroutine coro-test) [1 2 3]))) +(defn test-decorated-defn/a [] + (defn decorator [func] (fn/a [] (/ (await (func)) 2))) + + #@(decorator + (defn/a coro-test [] + (await (sleep 0)) + 42)) + (assert (= (run-coroutine coro-test) 21))) + + (defclass AsyncWithTest [] (defn --init-- [self val] (setv self.val val)