尽管“ py.typed”
我正在使用诗歌
维护两个存储库,让我们称它们为repo_main
和repo_depentencyency
。他们俩都使用python 3.8
并具有mypy
(v0.942
)键入提示,并且正如名称所暗示的那样,repo_main 取决于
repo_depentency
。现在,当我在repo_main
上运行mypy
时,repo_dependentys的类型提示
被忽略并有效地将其视为任何 s。然后,我尝试将
py.typed
文件添加到repo_depentency
的每个子包中,而没有结果,类型仍然被忽略。 repo_depentency
的结构看起来有点像这样:
repo_dependency/
- repo_dependency/
- package1/
- __init__.py
- py.typed
- stuff.py
- package2/
- __init__.py
- py.typed
- stuff.py
- __init__.py
- py.typed
- other_non_distributed_things/
知道我可能做错了什么?
I am maintaining two repositories using poetry
, let's call them repo_main
and repo_dependency
. Both of them use python 3.8
and have mypy
(v0.942
) type hints and, as the names suggest, repo_main
depends on repo_dependency
. Now, when I run mypy
on repo_main
, the type hints of repo_dependency
get de-facto ignored and effectively treated as Any
s. I then tried adding a py.typed
file into every sub-package of repo_dependency
with no results, the types are still ignored. The structure of repo_dependency
looks somewhat like this:
repo_dependency/
- repo_dependency/
- package1/
- __init__.py
- py.typed
- stuff.py
- package2/
- __init__.py
- py.typed
- stuff.py
- __init__.py
- py.typed
- other_non_distributed_things/
Any idea what I might be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,这一点根本不清楚。添加
py.typed
文件不会让mypy查看包源以获取类型信息。 Mypy只会在存根文件中查找此类信息(外部软件包)。另外,将.pyi
包源旁边的存根文件放置在包源旁边不会说服Mypy查看这些存根文件。因此,人们需要添加
py.typed
file 和.pyi
stub文件。对我来说,这似乎是非常多余的,这就是为什么它如此不直觉以至于我们都需要两者。
This is, to me, not at all clear from the documentation. Adding a
py.typed
file doesn’t get mypy to look into the package sources for type information. Mypy will only look in stub files for such information (for external packages). Also, putting.pyi
stub files next to the package sources will not convince mypy to look into those stub files.So, one needs to add both the
py.typed
file and the.pyi
stub files.To me this seems terribly redundant, which is why it’s so unintuitive that we need both.