📝 测试文档部署

This commit is contained in:
2024-08-28 21:27:52 +08:00
parent 0320f13ae9
commit de42d34e38
16 changed files with 493 additions and 426 deletions

View File

@@ -139,18 +139,18 @@ class AstParser:
)
for default in node.args.defaults
],
return_=ast.unparse(node.returns).strip() if node.returns else TypeHint.NO_TYPEHINT,
return_=ast.unparse(node.returns).strip() if node.returns else TypeHint.NO_RETURN,
decorators=[ast.unparse(decorator).strip() for decorator in node.decorator_list],
is_async=isinstance(node, ast.AsyncFunctionDef),
src=ast.unparse(node).strip()
))
elif isinstance(node, (ast.Assign, ast.AnnAssign)):
if not self._is_module_level_variable(node):
if not self._is_module_level_variable2(node):
# print("变量不在模块级别", ast.unparse(node))
continue
else:
print("变量在模块级别", ast.unparse(node))
pass
if isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name):
@@ -180,7 +180,39 @@ class AstParser:
return False
return True
def _is_module_level_variable(self, node: ast.Assign):
def _is_module_level_variable(self, node: ast.Assign | ast.AnnAssign):
"""在类方法或函数内部的变量不会被记录"""
# for parent in ast.walk(self.tree):
# if isinstance(parent, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
# if node in parent.body:
# return False
# else:
# for sub_node in parent.body:
# if isinstance(sub_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
# if node in sub_node.body:
# return False
# return True
# 递归检查
def _check(_node, _parent):
if isinstance(_parent, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
if _node in _parent.body:
return False
else:
for sub_node in _parent.body:
if isinstance(sub_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
return _check(_node, sub_node)
return True
for parent in ast.walk(self.tree):
if not _check(node, parent):
return False
return True
def _is_module_level_variable2(self, node: ast.Assign | ast.AnnAssign) -> bool:
"""
检查变量是否在模块级别定义。
"""
for parent in ast.walk(self.tree):
if isinstance(parent, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
if node in parent.body:

View File

@@ -119,7 +119,7 @@ class FunctionNode(BaseModel):
kw_defaults: list[ConstantNode] = []
defaults: list[ConstantNode] = []
return_: str = Field(TypeHint.NO_RETURN, alias="return")
return_: str = TypeHint.NO_RETURN
decorators: list[str] = []
src: str
is_async: bool = False
@@ -153,8 +153,8 @@ class FunctionNode(BaseModel):
"""
self.complete_default_args()
PREFIX = "" * indent
if is_classmethod:
PREFIX = "- #"
# if is_classmethod:
# PREFIX = "- #"
md = ""
@@ -211,11 +211,14 @@ class FunctionNode(BaseModel):
md += ", ".join(args) + ")"
if self.return_ != TypeHint.NO_RETURN:
md += f" -> {self.return_}"
md += "`\n\n" # code end
"""此处预留docstring"""
if self.docs is not None:
md += f"\n{self.docs.markdown(lang, indent)}\n"
md += f"\n{self.docs.markdown(lang, indent, is_classmethod)}\n"
else:
pass
# 源码展示