使用泛型基础修复多重继承 类

Fix multiple inheritance with generic base classes

from typing import Generic, TypeVar, Any

R = TypeVar('R')
X = TypeVar('X')

class SizedIterator(Generic[X]):
    def __init__(self) -> None:
        pass

class TfmIterator(Generic[R],  SizedIterator):
    def __init__(self) -> None:
        pass

以上是 https://github.com/autorope/donkeycar/blob/dev/donkeycar/pipeline/sequence.py.

中代码的简化版本

显然该代码在 Python 3.6 and/or 3.7 中运行良好。但是,当我在 Python 3.9 中尝试 运行 时出现以下错误:

Traceback (most recent call last):
  File "/Users/Shared/Personal/mycar/simple1.py", line 10, in <module>
    class TfmIterator(Generic[R],  SizedIterator):
TypeError: Cannot create a consistent method resolution
order (MRO) for bases Generic, SizedIterator

我的问题是如何保持相同类型的提示而不 运行进入 MRO 错误?

你可以试试这个:

from typing import Generic, TypeVar, Any

R = TypeVar('R')
X = TypeVar('X')


class SizedIterator(Generic[X]):
    def __init__(self) -> None:
        pass


class NewIterator(Generic[R]):
    def __init__(self) -> None:
        pass


class TfmIterator(NewIterator, SizedIterator):
    def __init__(self) -> None:
        pass