From e52d3a36510f646eca8f45a1cba20e34001a461e Mon Sep 17 00:00:00 2001 From: yazawazi <47273265+Yazawazi@users.noreply.github.com> Date: Sat, 18 May 2024 14:04:18 +0800 Subject: [PATCH] feat: support docstring in runtime ast --- backend/funix/decorator/runtime.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/funix/decorator/runtime.py b/backend/funix/decorator/runtime.py index 48cf1a0..7a44477 100644 --- a/backend/funix/decorator/runtime.py +++ b/backend/funix/decorator/runtime.py @@ -181,6 +181,13 @@ def visit_FunctionDef(self, node: FunctionDef): type_ignores=[], ) + node_doc_string = None + if node.body: + if isinstance(node.body[0], Expr) and isinstance( + node.body[0].value, Constant + ): + node_doc_string = node.body[0].value.value + if node.name == "__init__": # new_module.body[0].decorator_list[0].keywords = [ # keyword(arg="title", value=Constant(value=self._cls_name)) @@ -204,6 +211,8 @@ def visit_FunctionDef(self, node: FunctionDef): ) ) ] + if node_doc_string: + function.body.insert(0, Expr(value=Constant(value=node_doc_string))) else: if is_static_method: function.body = [ @@ -219,6 +228,8 @@ def visit_FunctionDef(self, node: FunctionDef): ) ) ] + if node_doc_string: + function.body.insert(0, Expr(value=Constant(value=node_doc_string))) else: function.body = [ Assign( @@ -242,6 +253,8 @@ def visit_FunctionDef(self, node: FunctionDef): ) ), ] + if node_doc_string: + function.body.insert(0, Expr(value=Constant(value=node_doc_string))) globals()["__funix__module__"] = funix globals()[self._cls_name] = self._cls