首页 > 解决方案 > Python同时继承abc.ABC和typing.SupportsBytes

问题描述

我是编写类型化 Python 代码的新手,但我有其他语言的 OOP 经验。我想创建一个抽象类,也可以使用bytes函数进行序列化。我正确实现__bytes__了方法,但我想明确声明此方法可用。

我的类声明如下所示:

from abc import ABC, abstractmethod
from typing import SupportsBytes

class cls(ABC, SupportsBytes):

Python 抱怨TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases.

有什么办法可以改变我的声明以使其工作而不会放弃祖先abc.ABCtyping.SupportsBytes祖先?

我曾尝试替换abc.ABCwithabc.ABCMeta以及调用register函数 on cls,但到目前为止,每次尝试都失败了,并出现了自己的错误。

编辑:我使用 Python 3.7.7

标签: pythonabcpython-typing

解决方案


推荐阅读