也许你正在考虑这样的事情。
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 将推断迭代器的类型。