【问题标题】:How to distribute type hints to PyPi?如何向 PyPi 分发类型提示?
【发布时间】:2016-01-02 21:51:21
【问题描述】:

我一直致力于将 Python 3.5 类型提示添加到响应库中。但是当我测试发布sdistbdist_wheel 时,它不会安装我的.pyi 文件。我可以看到它是分发的一部分,但仅此而已。

你可以在这里看到我在我的 repo 中得到了什么:https://github.com/gaqzi/responses/tree/feature/type-hints-file

我阅读了 mentions that stub files should be distributable. 的 PEP484,但我似乎无法弄清楚如何。 :)

是否有问题是因为响应没有创建包?它只是一个单独的模块文件,这就是它没有正确添加的原因?

我在构建包时看到的内容:

% python setup.py sdist
running sdist
running egg_info
writing requirements to responses.egg-info/requires.txt
writing top-level names to responses.egg-info/top_level.txt
writing responses.egg-info/PKG-INFO
writing dependency_links to responses.egg-info/dependency_links.txt
reading manifest file 'responses.egg-info/SOURCES.txt'
writing manifest file 'responses.egg-info/SOURCES.txt'
running check
warning: check: missing meta-data: if 'author' supplied, 'author_email' must be supplied too
creating responses-0.6.0
creating responses-0.6.0/responses.egg-info
making hard links in responses-0.6.0...
hard linking README.rst -> responses-0.6.0
hard linking responses.py -> responses-0.6.0
hard linking responses.pyi -> responses-0.6.0
hard linking setup.cfg -> responses-0.6.0
hard linking setup.py -> responses-0.6.0
hard linking responses.egg-info/PKG-INFO -> responses-0.6.0/responses.egg-info
hard linking responses.egg-info/SOURCES.txt -> responses-0.6.0/responses.egg-info
hard linking responses.egg-info/dependency_links.txt -> responses-0.6.0/responses.egg-info
hard linking responses.egg-info/not-zip-safe -> responses-0.6.0/responses.egg-info
hard linking responses.egg-info/requires.txt -> responses-0.6.0/responses.egg-info
hard linking responses.egg-info/top_level.txt -> responses-0.6.0/responses.egg-info
copying setup.cfg -> responses-0.6.0
Writing responses-0.6.0/setup.cfg
Creating tar archive
removing 'responses-0.6.0' (and everything under it)

安装包后我得到了这个:

% pip install dist/responses-0.6.0.tar.gz
[...snip...]
Installing collected packages: responses
Successfully installed responses-0.6.0
% pwd
/Users/ba/.virtualenvs/responses/lib/python3.5/site-packages
% ls responses*
responses.py

responses-0.6.0.dist-info:
DESCRIPTION.rst METADATA        RECORD          WHEEL           metadata.json   top_level.txt

【问题讨论】:

  • 似乎将您的.pyi 文件包含在MANIFEST.in 作品中
  • 假设将其添加到带有include_package_data=Truepackage_data 行是MANIFEST.in 的可接受替代品,不,这是行不通的。参见github.com/LuminosoInsight/ordered-set/pull/42,它同时添加了ordered_set.pyipy.typed,但是类型没有被打包。

标签: python setuptools type-hinting


【解决方案1】:

根据the mypy docs,您应该在setup.py 中将package_data={"my_package": ["py.typed", "foo.pyi"]} 作为参数传递给setup。注意"foo.pyi"是从要分发的包的根目录到 存根文件 (docs)。

我创建了一个示例存储库,您可以在其中进行测试,地址为 https://github.com/SKalt/stub_distrib_demo

【讨论】:

  • 您还可以全局化所有 .pyi 文件:["py.typed", "*.pyi", "**/*.pyi"].
  • 如果你想使用find_packages: package_data={package: ["py.typed", ".pyi", "**/.pyi"]包在 setuptools.find_packages()}
猜你喜欢
  • 2016-07-04
  • 2021-10-17
  • 2013-08-22
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多