mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 07:27:15 +08:00
fix: type hints
This commit is contained in:
+6
-7
@@ -2,7 +2,7 @@ import ast
|
|||||||
import dis
|
import dis
|
||||||
import inspect
|
import inspect
|
||||||
import textwrap
|
import textwrap
|
||||||
from types import FunctionType, MethodType
|
from types import FunctionType
|
||||||
from typing import Any, Callable, get_type_hints
|
from typing import Any, Callable, get_type_hints
|
||||||
|
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ class ObjectUtils:
|
|||||||
return len(list(parameters.keys()))
|
return len(list(parameters.keys()))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def check_method(func: FunctionType | MethodType) -> bool:
|
def check_method(func: Callable[..., Any]) -> bool:
|
||||||
"""
|
"""
|
||||||
检查函数是否已实现
|
检查函数是否已实现
|
||||||
"""
|
"""
|
||||||
@@ -60,10 +60,9 @@ class ObjectUtils:
|
|||||||
# 跳过 docstring 或 ...
|
# 跳过 docstring 或 ...
|
||||||
if isinstance(stmt, ast.Expr):
|
if isinstance(stmt, ast.Expr):
|
||||||
expr = stmt.value
|
expr = stmt.value
|
||||||
if isinstance(expr, ast.Constant) and isinstance(expr.value, str):
|
if isinstance(expr, ast.Constant):
|
||||||
continue
|
if isinstance(expr.value, str) or expr.value is Ellipsis:
|
||||||
if isinstance(expr, ast.Constant) and expr.value is Ellipsis:
|
continue
|
||||||
continue
|
|
||||||
# 检查 raise NotImplementedError
|
# 检查 raise NotImplementedError
|
||||||
if isinstance(stmt, ast.Raise):
|
if isinstance(stmt, ast.Raise):
|
||||||
exc = stmt.exc
|
exc = stmt.exc
|
||||||
@@ -77,7 +76,7 @@ class ObjectUtils:
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(err)
|
print(err)
|
||||||
# 源代码分析失败时,进行字节码分析
|
# 源代码分析失败时,进行字节码分析
|
||||||
code_obj = func.__code__
|
code_obj = func.__code__ # type: ignore[attr-defined]
|
||||||
instructions = list(dis.get_instructions(code_obj))
|
instructions = list(dis.get_instructions(code_obj))
|
||||||
# 检查是否为仅返回None的简单结构
|
# 检查是否为仅返回None的简单结构
|
||||||
if len(instructions) == 2:
|
if len(instructions) == 2:
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class ObjectUtilsTest(TestCase):
|
|||||||
def not_implemented_function():
|
def not_implemented_function():
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def not_implemented_function_no_call():
|
def not_implemented_function_with_call():
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def multiple_lines_async_def(_param1: str,
|
async def multiple_lines_async_def(_param1: str,
|
||||||
@@ -36,6 +36,6 @@ class ObjectUtilsTest(TestCase):
|
|||||||
self.assertFalse(ObjectUtils.check_method(docstring_function))
|
self.assertFalse(ObjectUtils.check_method(docstring_function))
|
||||||
self.assertFalse(ObjectUtils.check_method(ellipsis_function))
|
self.assertFalse(ObjectUtils.check_method(ellipsis_function))
|
||||||
self.assertFalse(ObjectUtils.check_method(not_implemented_function))
|
self.assertFalse(ObjectUtils.check_method(not_implemented_function))
|
||||||
self.assertFalse(ObjectUtils.check_method(not_implemented_function_no_call))
|
self.assertFalse(ObjectUtils.check_method(not_implemented_function_with_call))
|
||||||
self.assertFalse(ObjectUtils.check_method(multiple_lines_async_def))
|
self.assertFalse(ObjectUtils.check_method(multiple_lines_async_def))
|
||||||
self.assertTrue(ObjectUtils.check_method(empty_function))
|
self.assertTrue(ObjectUtils.check_method(empty_function))
|
||||||
|
|||||||
Reference in New Issue
Block a user