我有一个文件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
,但这不起作用。
您需要允许其中一个重载的默认参数,并在ffill
方法中为self
设置正确的类型。
:
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, = 49要点da369f6343543769eed2060fa61639