如何正确为自定义overload装饰器提供类型提示?
如何让自定义重载装饰器实现标准库
overload的类型提示效果? 我有一个可正常工作的自定义重载装饰器,但无法提供和标准库typing.overload一致的类型提示。选择自定义装饰器的原因如下:
- 标准库
overload仅作为装饰标记使用,没有实际的函数调度逻辑 - 类似
multipledispatch.dispatch的第三方库方案不属于标准库范畴
标准库overload的类型提示表现
使用标准库overload时,VSCode中会显示所有重载的类型提示:
from typing import overload class Foo : @overload def bar( self , value : int ) -> int : ... @overload def bar( self , value : str ) -> str : ...

自定义重载装饰器的问题
但使用自定义重载装饰器时,编辑器只会显示最后加载的方法的类型提示:
def custom_overload( * types ) : # 调度逻辑实现 ... class Foo : @custom_overload( int ) def bar( self , value : int ) -> int : ... @custom_overload( str ) def bar( self , value : str ) -> str : ...

请问如何让自定义装饰器装饰的函数实现与标准库overload一致的类型提示?这是否和重载/调度装饰器的文档标注问题相关?
内容的提问来源于stack exchange,提问作者Micah
相关产品推荐
相关产品推荐

