feat: +unit test

fixbug: func has no value
This commit is contained in:
莘权 马 2023-12-29 14:52:21 +08:00
parent 5510df5f96
commit 6f039d004d
28 changed files with 367 additions and 552 deletions

View file

@ -528,18 +528,18 @@ def role_raise_decorator(func):
@handle_exception
async def aread(file_path: str) -> str:
async def aread(filename: str | Path, encoding=None) -> str:
"""Read file asynchronously."""
async with aiofiles.open(str(file_path), mode="r") as reader:
async with aiofiles.open(str(filename), mode="r", encoding=encoding) as reader:
content = await reader.read()
return content
async def awrite(filename: str | Path, data: str):
async def awrite(filename: str | Path, data: str, encoding=None):
"""Write file asynchronously."""
pathname = Path(filename)
pathname.parent.mkdir(parents=True, exist_ok=True)
async with aiofiles.open(str(pathname), mode="w", encoding="utf-8") as writer:
async with aiofiles.open(str(pathname), mode="w", encoding=encoding) as writer:
await writer.write(data)

View file

@ -49,6 +49,14 @@ def get_docstring_statement(body: DocstringNode) -> cst.SimpleStatementLine:
return statement
def has_decorator(node: DocstringNode, name: str) -> bool:
return hasattr(node, "decorators") and any(
(hasattr(i.decorator, "value") and i.decorator.value == name)
or (hasattr(i.decorator, "func") and hasattr(i.decorator.func, "value") and i.decorator.func.value == name)
for i in node.decorators
)
class DocstringCollector(cst.CSTVisitor):
"""A visitor class for collecting docstrings from a CST.
@ -82,7 +90,7 @@ class DocstringCollector(cst.CSTVisitor):
def _leave(self, node: DocstringNode) -> None:
key = tuple(self.stack)
self.stack.pop()
if hasattr(node, "decorators") and any(i.decorator.value == "overload" for i in node.decorators):
if has_decorator(node, "overload"):
return
statement = get_docstring_statement(node)
@ -127,9 +135,7 @@ class DocstringTransformer(cst.CSTTransformer):
key = tuple(self.stack)
self.stack.pop()
if hasattr(updated_node, "decorators") and any(
(i.decorator.value == "overload") for i in updated_node.decorators
):
if has_decorator(updated_node, "overload"):
return updated_node
statement = self.docstrings.get(key)