无法从键入中导入协议

2024-04-25 19:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚在Windows10上安装了Python3.7-32。在

我想尝试一下协议python方法,接下来我要做的是:

文件测试_协议.py只有一行:

from typing import Protocol

然后:

^{pr2}$

我有下一条需要解释的错误消息:

Traceback (most recent call last):
  File "test_protocols.py", line 1, in <module> 
    from typing import Protocol
ImportError: cannot import name 'Protocol' from 'typing' (C:\Programing\Python\Python37-32\lib\typing.py)

我做错了什么?在

也许我读错了PEP-0544,但从我的观点来看,我做的和记录的一样。在


Tags: 文件方法frompyimport消息协议typing
2条回答

PEP 544implementation部分,它说

The mypy type checker fully supports protocols (modulo a few known bugs). This includes treating all the builtin protocols, such as Iterable structurally. The runtime implementation of protocols is available in typing_extensions module on PyPI.

因此,在代码中添加from typing_extensions import Protocol。在

截至2019年1月20日,PEP 544的状态为Draft。据我所知,它还没有在CPython中实现。在

相关问题 更多 >