我有一个文件
t.py
,其中有一个类 Animal
和一个子类 Cat
。两者都有方法 foo
,该方法根据布尔值 inplace
的值具有不同的返回类型。
这是文件的完整代码:
# t.py
from __future__ import annotations
from typing import TypeVar, Optional, overload, Literal
CatOrDog = TypeVar("CatOrDog", bound="Animal")
class Animal:
@overload
def foo(self: CatOrDog, inplace: Literal[False], bar) -> CatOrDog:
...
@overload
def foo(self: CatOrDog, inplace: Literal[True], bar) -> None:
...
def foo(
self: CatOrDog, inplace: bool = False, bar=None
) -> Optional[CatOrDog]:
...
def ffill(self) -> Optional[CatOrDog]:
return self.foo(bar="a")
class Cat(Animal):
@overload
def foo(self, inplace: Literal[False], bar) -> Cat:
...
@overload
def foo(self, inplace: Literal[True], bar) -> None:
...
def foo(self, inplace: bool = False, bar=None) -> Optional[Cat]:
...
如果我运行
mypy
,我会得到
$ mypy t.py
t.py:23: error: No overload variant of "foo" of "Animal" matches argument type "str"
t.py:23: note: Possible overload variants:
t.py:23: note: def foo(self, inplace: Literal[False], bar: Any) -> Animal
t.py:23: note: def foo(self, inplace: Literal[True], bar: Any) -> None
Found 1 error in 1 file (checked 1 source file)
我怎样才能正确地超载
foo
,以便我可以打电话给self.foo(bar="a")
?我尝试过设置bar: Any
,但不起作用。
您需要允许其中一个重载使用默认参数,并在
self
方法中为 ffill
设置正确的类型。
如:
from __future__ import annotations
from typing import TypeVar, Optional, overload, Literal
CatOrDog = TypeVar("CatOrDog", bound="Animal")
class Animal:
@overload
def foo(self: CatOrDog, inplace: Literal[False]=..., bar=...) -> CatOrDog:
...
@overload
def foo(self: CatOrDog, inplace: Literal[True], bar=...) -> None:
...
def foo(
self: CatOrDog, inplace: bool = False, bar=None
) -> Optional[CatOrDog]:
...
def ffill(self: CatOrDog) -> Optional[CatOrDog]:
return self.foo(bar="a")
class Cat(Animal):
@overload
def foo(self, inplace: Literal[False]=..., bar=...) -> Cat:
...
@overload
def foo(self, inplace: Literal[True], bar=...) -> None:
...
def foo(self, inplace: bool = False, bar=None) -> Optional[Cat]:
...
参见https://mypy-play.net/?mypy=latest&python=3.9&gist=49da369f6343543769eed2060fa61639