首页 > 编程问答 >如何测试 python 类型协议是另一个协议的子类?

如何测试 python 类型协议是另一个协议的子类?

时间:2024-07-30 14:16:42浏览次数:9  
标签:python inheritance protocols python-typing

该问题的明显解决方案是使用 issubclass ,但这会引发 TypeError (使用Python 3.6.7),例如

>>> from typing_extensions import Protocol
>>> class ProtoSubclass(Protocol):
...     pass
... 
>>> issubclass(ProtoSubclass, Protocol)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/envs/airflow/lib/python3.6/site-packages/typing_extensions.py", line 1265, in __subclasscheck__
    raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime protocols
>>> from typing_extensions import runtime
>>> @runtime
... class ProtoSubclass(Protocol):
...     pass
... 
>>> issubclass(ProtoSubclass, Protocol)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/envs/airflow/lib/python3.6/site-packages/typing_extensions.py", line 1265, in __subclasscheck__
    raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime protocols

你说的没错,在 Python 3.6 中使用 issubclass 直接检查协议子类关系会引发 TypeError 。这是因为在 Python 3.7 之前的版本中,协议的子类检查机制尚未完全实现。

不过,你可以通过以下两种方式来变通解决这个问题:

1. 使用 typing_inspect 库:

typing_inspect 库提供了一些函数来检查类型提示,包括协议。你可以使用 typing_inspect.is_subtype 函数来检查一个协议是否是另一个协议的子类型。

from typing_extensions import Protocol, runtime
from typing_inspect import is_subtype

@runtime
class ProtoBase(Protocol):
    def method_base(self): ...

@runtime
class ProtoSubclass(ProtoBase):
    def method_sub(self): ...

print(is_subtype(ProtoSubclass, ProtoBase))  # 输出:True
print(is_subtype(ProtoBase, ProtoSubclass))  # 输出:False

2. 使用自定义函数检查协议成员:

由于协议本质上是定义了一组方法,你可以编写一个自定义函数来检查一个协议是否包含另一个协议的所有方法。

from typing_extensions import Protocol, runtime

@runtime
class ProtoBase(Protocol):
    def method_base(self): ...

@runtime
class ProtoSubclass(ProtoBase):
    def method_sub(self): ...

def is_protocol_subclass(sub_proto, base_proto):
    base_methods = set(getattr(base_proto, '__abstractmethods__', []))
    sub_methods = set(getattr(sub_proto, '__abstractmethods__', []))
    return base_methods.issubset(sub_methods)

print(is_protocol_subclass(ProtoSubclass, ProtoBase))  # 输出:True
print(is_protocol_subclass(ProtoBase, ProtoSubclass))  # 输出:False

需要注意的是,这两种方法都有一些局限性。 typing_inspect 库需要额外安装,而自定义函数则需要你手动维护协议方法的检查逻辑。

总的来说,如果你正在使用 Python 3.7 或更高版本,那么 issubclass 函数可以直接用于检查协议的子类关系。但如果你必须使用 Python 3.6,那么以上两种方法可以帮助你解决这个问题。

标签:python,inheritance,protocols,python-typing
From: 59461689

相关文章

  • Python:指定与继承一起使用的类方法的返回类型
    我一直在尝试了解如何在Python中指定类方法的返回类型,以便即使对于子类也能正确解释它(例如在我的Sphinx文档中)。假设我有:classParent:@classmethoddefa_class_method(cls)->'Parent':returncls()classChild(Parent):pass什么如......
  • python使用SMTP功能发送邮件
    网页格式发送for_email.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><style>h1{color:brown;}p{margin:5px;color:purple......
  • python之代码简化式(列表、字典生成式,递归函数,迭代器(iter)和生成器(yield)、匿名函数(
    文章目录前言1、列表、字典生成式2、递归函数2.1python中代码的递归深度(扩展)3、拓展:迭代器和生成器3.1迭代器(iter)3.2生成器(yield)4、匿名函数(lambda)4.1map函数4.2reduce函数(较少使用)4.3filter函数前言本文主要讲解一些简化代码格式的一些方法,方便大家更好的......
  • Python:在 Protocol 和 TypedDict 之间共享类型注释
    举这个简单的例子:from__future__importannotationsimporttypingastclassMyType:def__init__(self,s:str,i:int)->None:self.s=sself.i=iclassMyProto(t.Protocol):s:stri:intclassMyDict(t.TypedDict):......
  • PIL 和 python 静态类型
    我有一个函数参数,它可以接受图像的多种类型:defsomefunc(img:Union[np.array,Image,Path,str]):PILImage在这种情况下抛出以下异常:TypeError:Union[arg,...]:eachargmustbeatype.Got<module'PIL.Image'from...进一步检查图像对象后这才有......
  • 学会用Python爬取小说网站,想看什么就爬什么,广告也不用看了~
    今天以爬取笔趣阁小说网站为例,练习Python爬虫技术。通过这个爬虫,可以完成批量爬取一本小说的所有章节,并将所有章节内容按顺序保存到一个txt文档内,下面我们就开始吧。首先,百度搜索“笔趣阁”,发现有很多网站都叫笔趣阁。我们可以随便挑选一个网站尝试,本文我以‘https://......
  • 计算机毕业设计django+vue《Python数据分析》的教学系统【开题+论文+程序】
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景在当今数字化时代,数据分析已成为各行各业不可或缺的技能之一,而Python作为数据分析领域的首选语言,其重要性日益凸显。然而,传统的教学模式在......
  • 如何将多个变量分配给 python 函数中的单个参数?
    我正在尝试编写一个程序,如果可能的话,它需要一个三项式并对其进行因式分解。每当用户输入A、B和C时,三项式应该通过Factor(product,summation)函数获取,但我似乎无法弄清楚如何将A和C分配给乘积arg,将B分配给我尝试在函数外部声明不同的变量,product=(a*c)和summati......
  • python - 从文本生成音乐
    请给我一些建议为了解释一下,我输入“深度睡眠的睡眠音乐”,它将返回一个wav文件:https://www.youtube.com/watch?v=1wAdQhFJy54或者我给出一个wav文件,它会返回相同的现在这是我尝试过的:https://github.com/facebookresearch/audiocraft......
  • 从零开始的Python开发日记(7):短信验证功能开发流程
    短信验证功能开发流程在开发一个包含登录、注册以及短信验证的功能时,你需要遵循一个系统的开发流程。以下是实现这一功能的基本步骤,包括所需的技术和代码示例。1.环境配置首先,确保你的开发环境已经配置好,并安装了必要的库和工具。pipinstallfastapiuvicornsqlalche......