示例如下,需要确保IDE类型判断器或reveal_type
能够正确识别k
、j
和i
类型.
也许有某种方法可以建议输入args
是空的tuple
,kwargs
是空的dict
,那么返回值就是tuple[int]
?
from typing import Union, overload
def test(*args: int, **kwargs: str) -> Union[int, str, tuple[int]]:
if args:
return 5
if kwargs:
return "5"
return (5,)
# now all are Union[int, str, tuple[int]]
k = test(1)
j = test(i="1")
i = test()
reveal_type(k) # should be int
reveal_type(j) # should be str
reveal_type(i) # should be tuple[int]