我试图用@overload不同的方式来传达调用函数的不同方式,但是在代码中用简单的else语句轻松传达的内容在类型注释中是不可能的。如果没有“else”,MyPy(正确地)会抱怨重载版本不匹配(例如,请参见下面的代码段)。
error: Overloaded function signatures 1 and 2 overlap with incompatible return types
我是否正确理解此问题没有好的解决方案?
例如。这是一个简单的例子:
ListOrTuple = TypeVar("ListOrTuple", List, Tuple)
# unfortunately, typing doesn't support "anything else" at the moment
# https://github.com/python/typing/issues/599#issuecomment-586007066
AnythingElse = TypeVar("AnythingElse")
# what I would like to have is something like AnythingElse= TypeVar("AnythingElse", Not[List,Tuple])
@overload
def as_list(val: ListOrTuple) -> ListOrTuple:
...
@overload
def as_list(val: AnythingElse) -> List[AnythingElse]:
...
def as_list(val):
"""Return list/tuple as is, otherwise wrap in a list
>>> as_list("test")
['test'] …Run Code Online (Sandbox Code Playgroud)