12

我正在使用pythonmypy中的typing模块。想象一下我有一个泛型类型:

ContainerT = TypeVar('ContainerT')

class Thingy(Generic[ContainerT]):
    pass

但是,我想在与 TypeVar 关联的具体类型中获取另一种类型,如下例所示:

class SomeContainer(object):
    Iterator = SomeCustomIterator

ContainerT = TypeVar('ContainerT')

class Thingy(Generic[ContainerT]):
    def foo(self) -> ContainerT.Iterator:
        pass

我收到一条错误消息,说TypeVar没有成员Iterator。有没有另一种方法来重新制定这个,所以我可以将一种类型与另一种类型联系起来?

谢谢你。

4

1 回答 1

2

也许你正在考虑这样的事情。

from __future__ import annotations

from abc import abstractmethod
from typing import Generic, TypeVar, Iterator, Protocol

_Iterator = TypeVar('_Iterator', bound=Iterator, covariant=True)

class SomeContainer:
    iterator: Iterator[int]

    def __init__(self, iterator: Iterator[int]):
        self.iterator = iterator

    def get_iterator(self) -> Iterator[int]:
        return self.iterator


class SomeContainerProtocol(Protocol[_Iterator]):
    @abstractmethod
    def __init__(self, iterator: _Iterator):
        pass

    @abstractmethod
    def get_iterator(self) -> _Iterator:
        pass


_SomeContainer = TypeVar('_SomeContainer', bound='SomeContainerProtocol', covariant=True)


class Thingy(Generic[_SomeContainer]):
    container: _SomeContainer

    def __init__(self, container: _SomeContainer):
        self.container = container

    def foo(self: Thingy[SomeContainerProtocol[_Iterator]]) -> _Iterator:
        pass

    def bar(self) -> _SomeContainer:
        pass

thingy = Thingy(SomeContainer(range(10).__iter__()))
reveal_type(thingy) # Revealed type is '...Thingy[...SomeContainer*]'
reveal_type(thingy.foo) # Revealed type is 'def () -> typing.Iterator[builtins.int]'
reveal_type(thingy.bar) # Revealed type is 'def () ->  ...SomeContainer*'

您可以使用通用协议(从 Python 3.8 开始可用)来注释 self 来代替您的类型 var,然后 mypy 将推断迭代器的类型。

于 2020-06-12T18:37:30.780 回答