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