在泛型 class 内的泛型方法上绑定 TypeVar
Bound TypeVar on generic methods inside generic class
出于某种原因,此代码出现问题:
from typing import *
T = TypeVar("T", bound="Foo")
S = TypeVar("S")
class Foo(Generic[S]):
@classmethod
def func(cls: Type[T]) -> T:
return cls()
Mypy linter 将我发送到 def func
行,说 Unsupported type Type["T"]
。如果 Foo
未定义为通用 class.
,则不会发生这种情况
这是一个错误吗?我做错了什么?
我将 S
用于不同的方法,我希望稍后在 Foo
的子 class 中使用 T
和 Type[T]
.
我相信这是 bug in mypy。不幸的是,目前最好的解决方法是只向该行添加一个 # type: ignore
注释,也许连同一个 link 到相关问题。稍后,您可以检查 运行 mypy 是否已使用 --warn-unused-ignores
标志修复该警告。
出于某种原因,此代码出现问题:
from typing import *
T = TypeVar("T", bound="Foo")
S = TypeVar("S")
class Foo(Generic[S]):
@classmethod
def func(cls: Type[T]) -> T:
return cls()
Mypy linter 将我发送到 def func
行,说 Unsupported type Type["T"]
。如果 Foo
未定义为通用 class.
这是一个错误吗?我做错了什么?
我将 S
用于不同的方法,我希望稍后在 Foo
的子 class 中使用 T
和 Type[T]
.
我相信这是 bug in mypy。不幸的是,目前最好的解决方法是只向该行添加一个 # type: ignore
注释,也许连同一个 link 到相关问题。稍后,您可以检查 运行 mypy 是否已使用 --warn-unused-ignores
标志修复该警告。