小编bru*_*nns的帖子

Python 泛型和子类

我正在尝试向现有包添加类型注释,显然我错过了一些重要的东西。我有一个抽象的超类和子类。超类应该是通用的,而子类应该是针对特定类型的。这是我看到的一个简单示例,以及我希望看到的内容:

from typing import Generic, TypeVar

T = TypeVar("T")

class A(Generic[T]):
    def method(self, arg: T):
        ...

class B(A[int]):
    def method(self, arg):
        reveal_locals()
Run Code Online (Sandbox Code Playgroud)

预期(或至少希望):

GenericTest.py:11: note: Revealed local types are:
GenericTest.py:11: note:     arg: int
GenericTest.py:11: note:     self: Any
Run Code Online (Sandbox Code Playgroud)

得到了:

GenericTest.py:11: note: Revealed local types are:
GenericTest.py:11: note:     arg: Any
GenericTest.py:11: note:     self: Any
Run Code Online (Sandbox Code Playgroud)

python generics mypy

6
推荐指数
2
解决办法
3437
查看次数

标签 统计

generics ×1

mypy ×1

python ×1