[英]mypy can't find type hints for black
I have a file test.py
which only contains the line import black
.我有一个文件
test.py
,它只包含import black
行。 When I run mypy test.py
, I get the following error:当我运行
mypy test.py
,出现以下错误:
test.py:1: error: Skipping analyzing 'black': found module but no type hints or library stubs
test.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
However, when I look at the source for black
, it seems like everything has type hints.但是,当我查看
black
的来源时,似乎所有内容都有类型提示。 What do I need to do differently in mypy
to get it to use the type hints when I import black
?我需要在
mypy
做些什么不同的mypy
才能在我导入black
时使用类型提示?
Environment setup:环境设置:
conda create -n test python=3.7 -y
conda activate test
pip install black mypy
EDIT - From the link mentioned in the error message :编辑 - 从错误消息中提到的链接:
Mypy will not try inferring the types of any 3rd party libraries you have installed unless they either have declared themselves to be PEP 561 compliant stub package or have registered themselves on typeshed
Mypy 不会尝试推断您已安装的任何 3rd 方库的类型,除非它们已声明自己是符合 PEP 561 的存根包或已在 typeshed 上注册自己
I had assumed black
have satisfied one of these requirements, but it does not seem to be registered on typeshed .我假设
black
满足了这些要求之一,但它似乎没有在typeshed上注册。 From here , it seems that a package must have a py.typed
file to be PEP 561-compliant with inline annotations, and black
doesn't seem to have that either.从这里看来,一个包必须有一个
py.typed
文件才能符合 PEP 561 与内联注释,而black
似乎也没有。
My question still stands - given that the typing information is already there in black
, how can I (without needing a PR to be approved somewhere) get mypy
to use that?我的问题仍然存在 - 鉴于打字信息已经存在
black
,我怎样才能(不需要在某处批准 PR)让mypy
使用它?
I don't see any details about the py.typed
file - if that is just an empty file and only its existence matters, I assume I can create it somewhere and then typing will work?我没有看到有关
py.typed
文件的任何详细信息 - 如果这只是一个空文件并且只有它的存在很重要,我认为我可以在某处创建它然后打字会起作用吗?
EDIT - this has been fixed in black
now ( PR here , merged 5/8/2020), so in new versions no workaround is necessary.编辑 - 现在已修复为
black
(此处为PR ,于 2020 年 8 月 5 日合并),因此在新版本中无需解决方法。
Creating an empty file named py.typed
in the install directory is sufficient:在安装目录中创建一个名为
py.typed
的空文件就足够了:
cd ~/anaconda/envs/test/lib/python3.7/site-packages # or wherever your packages are installed
# black.py is directly in site-packages, but py.typed needs to be in a directory for the module
mkdir black
touch black/py.typed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.