python3 / mypy:如何管理存根文件?

发布于 2025-01-28 02:51:48 字数 921 浏览 1 评论 0原文

我创建了一个使用键入提示的软件包“ mypackage”。

我将MyPackage导入新模块(不是MyPackage的一部分),即MyModule,它也使用了键入提示。

我想在mymodule上使用mypy,但要得到:

错误:无法找到名为“ mypackage”的模块的实现或库存根

我一直在阅读有关存根的文档:

https://mypy.readthedocs.io/en/stable/stable/stubs.html#stub-files

然而,我还不清楚最好的做法是什么。

在这里有效但效率低下的东西。

  • 我创建一个软件包“ mypackage”。所有代码均已键入。
  • 我在软件包的根部调用uxgen,它将创建文件夹./out/mypackage,用
  • 我将此生成的目录移动到具有我的其他软件包的存根的文件夹,例如/path/to/stubs,
  • 我设置了该文件夹环境变量mypypath到/路径/到/存根

mypy mymodule.py

现在有效,但是这是做事的最好方法吗?

  • 有关类型的信息已经在源代码中,但是在PYI文件中,
  • 我们作为一个小组中的PYI文件重复了信息:每个人都必须生成存根并将其复制到其本地存根文件夹中,
  • 每个人都必须在代码时再次再生存根 很可能更新了

,我缺少一些东西。是否有更简单的方法将mypy应用于mymodule?

I created a package "mypackage" that uses typing hints.

I import mypackage in a new module (not part of mypackage), mymodule, that also used typing hints.

I would like to apply mypy on mymodule, but get:

error: Cannot find implementation or library stub for module named 'mypackage'

I have been reading the documentation about stubs:

https://mypy.readthedocs.io/en/stable/stubs.html#stub-files

Yet, it is unclear to me what the best practices are.

Here something that works, but feels inefficient.

  • I create a package "mypackage". All the code is typed.
  • I call stubgen at the root of the package, it creates the folder ./out/mypackage with the stub files
  • I move this generated directory to a folder which has the stubs for my other packages, e.g. /path/to/stubs
  • I set the environment variable MYPYPATH to /path/to/stubs

mypy mymodule.py

now works, but is this the best way of doing things?

  • the information about the types is already in the source code, yet the information is duplicated in pyi files
  • we work as a group: everybody has to generate the stubs and copy them to their local stubs folder
  • everybody has to regenerate the stubs again when the code is updated

Most likely, I am missing something. Is there a simpler way to apply mypy to mymodule ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

醉殇 2025-02-04 02:51:48

使用MyPy版本0.761时,显示了问题中的错误消息,但是0.950版本提供了更好的信息,因为它指向:

https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

依次指向:

https://mypy.readthedocs.io/en/stable/stable/instable/instable/instable/installed_ppackages.html#packages.html#instml#install#instalstalled-packages

它表明mypackage的设置应包括:

from distutils.core import setup

setup(
    # ...
    package_data={"mypackage": ["py.typed"]},
    #...
)

应该将空文件py.typ放在mypackage文件夹中。

安装了mypackage(pip)后,然后将mypy涂在mymodule上正常工作。

The error message in the question was displayed when using mypy version 0.761, but version 0.950 gives better information, as it points to:

https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

which points in turn to:

https://mypy.readthedocs.io/en/stable/installed_packages.html#installed-packages

It indicates that the setup.py of mypackage should include:

from distutils.core import setup

setup(
    # ...
    package_data={"mypackage": ["py.typed"]},
    #...
)

and an empty file py.typed should be placed in the mypackage folder.

After mypackage is (pip) installed, then applying mypy on mymodule works fine.

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