Код: Выделить всё
T = TypeVar("T", bound=BaseSample)
class MyExporter(Protocol[T]):
def get_sample(self) -> T:
...
def process_sample(self, sample: T) -> str:
...
Код: Выделить всё
def get_exporter(name: str) -> Union[MyExporter[SampleA], MyExporter[SampleB]]:
if name == "a":
return my_exporter_a
return my_exporter_b
Код: Выделить всё
exporter = get_exporter("a")
sample = exporter.get_sample()
output = exporter.process_sample(sample) # type checker complains here
< /code>
Я получаю следующие ошибки от Mypy: < /p>
Argument 1 to "process_sample" of "MyExporter" has incompatible type "Union[SampleA, SampleB]"; expected "SampleA"
Argument 1 to "process_sample" of "MyExporter" has incompatible type "Union[SampleA, SampleB]"; expected "SampleB"
Есть ли какой -нибудь способ, который не требует архитектурного пересмотра?
Подробнее здесь: https://stackoverflow.com/questions/779 ... s-problems