
解决使用自定义装饰器时 pylance 类型检测错误
使用自定义装饰器装饰函数时,pylance 可能无法识别装饰函数的返回类型,从而导致类型检测错误。以下是如何修改代码以解决此问题:
在自定义装饰器中,将装饰过的函数的返回类型显式指定为 callable[..., result]. 其中,result 是装饰过的函数的预期返回类型。
修改后的代码如下:
from typing import Callable
def execute(func) -youjiankuohaophpcn Callable[..., Result]:
def inner_wrapper(*args, **kwargs) -youjiankuohaophpcn Result[Any]:
with Session.begin() as session:
result = session.execute(func(*args, **kwargs))
return result
return inner_wrapper这样修改后,pylance 将识别 query_data_source 函数的返回类型为 result[any],消除警告。










