我有一个需要支持Python 3.7的项目,但是我想使用 typing.protocol
,在3.8中添加。为了支持3.7,我有一个较小的后备代码,该代码仅使用 object
:
import typing
_Protocol = getattr(typing, 'Protocol', object)
class Foo(_Protocol):
def bar(self) -> int:
pass
这一切正如我所期望的。问题是,在运行mypy时,我会收到以下错误:
test.py:5: error: Variable "test._Protocol" is not valid as a type
test.py:5: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
test.py:5: error: Invalid base class "_Protocol"
链接”变量vs类型别名该错误消息中的部分表示我应该注释 _protocol with :typing.type [object]
无效)或使用 typing.typealias
(直到Python 3.9才能使用)。
如何向Mypy表示 _protocol
有效作为一种类型?
我尝试的另一个解决方法是在:
if sys.version_info >= (3, 8):
_Protocol = typing.Protocol
else:
_Protocol = object
但是,这以相同的错误结束。
I have a project which needs to support Python 3.7, but I would like to use typing.Protocol
, which was added in 3.8. To support 3.7, I have a minor bit of fallback code which just uses object
:
import typing
_Protocol = getattr(typing, 'Protocol', object)
class Foo(_Protocol):
def bar(self) -> int:
pass
This all functions as I would expect. The issue is, when running MyPy, I get the following error:
test.py:5: error: Variable "test._Protocol" is not valid as a type
test.py:5: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
test.py:5: error: Invalid base class "_Protocol"
The linked "Variables vs type aliases" section in that error message indicates that I should annotate _Protocol
with : typing.Type[object]
(which does not work) or use typing.TypeAlias
(which isn't available until Python 3.9).
How can I indicate to MyPy that _Protocol
is valid as a type?
Another workaround I tried was in "Python version and system platform checks":
if sys.version_info >= (3, 8):
_Protocol = typing.Protocol
else:
_Protocol = object
However, this ends with the same error.
发布评论
评论(2)
使用
typing_extensions
和typing.type_checking
to Importtyping_extensions
仅在键入键入代码时。typing_extensions python版本并使用
typing.protocol
版本> = 3.8
:Use
typing_extensions
andtyping.TYPE_CHECKING
to importtyping_extensions
only when type-checking the code.typing_extensions checks the Python version and uses
typing.Protocol
for versions>=3.8
:Mypy似乎可以正确理解
协议
作为导入语句的一部分:Mypy仍然会标记
foo
作为Python 3.7中的类型规范,因此您需要使用类似的解决方法来使用它:值得注意的是,Mypy do 不了解更多聪明的东西,例如
_foo = foo = foo如果sys.version_info> =(3,8)否则打字.yany
- 您必须使用简单的格式。It seems that MyPy can properly understand
Protocol
as part of an import statement:MyPy will still flag usage of
Foo
as a type specification in Python 3.7, so you need a similar workaround for using it:It's worth noting that MyPy does not understand more clever things like
_Foo = Foo if sys.version_info >= (3, 8) else typing.Any
-- you have to use the simple format.