使用 Python 类型的 TypeVar 进行带绑定的通用类型返回

时间:2021-02-10 22:58:48

标签: python mypy

当尝试使用键入的 TypeVar 来启用具有返回类型的泛型时,我遇到了一个 mypy 错误,即在比较字典类型和预期返回时未考虑 bound 参数函数类型。

以下是我面临的情况的示例:

from typing import Dict, List, Type, TypeVar


class Bird:
    def call(self):
        print(self.sound)


class Chicken(Bird):
    def __init__(self):
        self.sound = "bok bok"


class Owl(Bird):
    def __init__(self):
        self.sound = "hoot hoot"


T = TypeVar("T", bound=Bird)


class Instantiator:
    def __init__(self, birds: List[Type[Bird]]):
        self._bird_map: Dict[Type[Bird], Bird] = {}
        for bird in birds:
            self._bird_map[bird] = bird()

    def get_bird(self, bird_type: Type[T]) -> T:
        return self._bird_map[bird_type]

运行 mypy 验证器将显示:temp.py:29: error: Incompatible return value type (got "Bird", expected "T")

Instantiator 用作一种“跟踪器”,用于实例化每种鸟类中的一种。当尝试根据类类型检索实例化对象时,这就是为什么需要使用泛型的原因,否则以后键入的字段会抱怨使用 Bird 类而不是 Chicken 或 {{ 之一1}}。

我在这里错误地使用了 Owl 吗?有没有不同的方法来处理结构?这是 mypy 的疏忽吗?

1 个答案:

答案 0 :(得分:2)

这是因为您定义了一个仅包含基类对象 Bird 的字典,但是在函数 get_bird 中,您试图返回基类类型的对象,而派生类可能是预期的。 Mypy 不会使 Base -> Derived 投射。

您可以使 __init__ 也成为通用函数。

T = TypeVar("T", bound=Bird)

class Instantiator():
    def __init__(self, birds: List[Type[T]]):
        self._bird_map: Dict[Type[T], T] = {}
        for bird in birds:
            self._bird_map[bird] = bird()

    def get_bird(self, bird_type: Type[T]) -> T:
        return self._bird_map[bird_type]

或者明确使用 cast:

class Instantiator:
    def __init__(self, birds: List[Type[Bird]]):
        self._bird_map: Dict[Type[Bird], Bird] = {}
        for bird in birds:
            self._bird_map[bird] = bird()

    def get_bird(self, bird_type: Type[T]) -> T:
        return cast(T, self._bird_map[bird_type])