我正在使用pytype(2019.10.17,到目前为止的最新版本)作为我的代码类型检查器来开发一个工具,它可以通过使用索引文件随机读取msgpack文件,该文件记录位置(msgpack文件中的偏移量) ) 每条消息(存储在 msgpack 中的值)。
在消息类型的多样性方面,我使用typing.TypeVar
泛型类型来实现。pytype 使用 TypeVar 时遇到问题。
Name: pytype
Version: 2019.10.17
Summary: Python type inferencer
Home-page: https://google.github.io/pytype
Author: None
Author-email: None
License: UNKNOWN
Location: /home/gaoyunpeng/miniconda3/envs/restore/lib/python3.6/site-packages
Requires: ninja, typed-ast, six, importlab, pyyaml, attrs
Required-by:
Python 3.6.4 :: Anaconda, Inc.
from typing import TypeVar
T = TypeVar('T')
def f(x: T):
print(x)
使用命令运行上述代码pytype main2.py
:
Computing dependencies
Analyzing 1 sources with 0 local dependencies
ninja: Entering directory `/home/gaoyunpeng/workspace/.pytype'
[1/1] check main2
FAILED: /home/gaoyunpeng/workspace/.pytype/pyi/main2.pyi
pytype-single --imports_info /home/gaoyunpeng/workspace/.pytype/imports/main2.imports --module-name main2 -V 3.6 -o /home/gaoyunpeng/workspace/.pytype/pyi/main2.pyi --analyze-annotated --nofail --quick /home/gaoyunp
eng/workspace/main2.py
File "/home/gaoyunpeng/workspace/main2.py", line 4, in <module>: Invalid type annotation 'T' [invalid-annotation]
Appears only once in the signature
For more details, see https://google.github.io/pytype/errors.html#invalid-annotation.
ninja: build stopped: subcommand failed.
如前所述https://google.github.io/pytype/errors.html#invalid-annotation
,这种情况是无效注释。
为什么代码不能通过pytype检查?