尽管“ py.typed”

发布于 2025-01-24 19:21:08 字数 808 浏览 1 评论 0原文

我正在使用诗歌维护两个存储库,让我们称它们为repo_mainrepo_depentencyency。他们俩都使用python 3.8并具有mypyv0.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 Anys. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2025-01-31 19:21:09

对我来说,这一点根本不清楚。添加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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文