自动创建 .pyi 文件? [英] Create .pyi files automatically?

查看:125
本文介绍了自动创建 .pyi 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自动创建 pyi 文件?

How can I automatically create the boilerplate code of pyi files?

我想为类型创建一个 pyi 文件提示如 pep484 中所述,其中包含所有方法名称.

I want to create a pyi file for type hinting as described in pep484 which contains all method names.

我不想要魔法.我想在自动创建文件后添加类型信息.

I don't want magic. I want to add the type information after auto creating the file.

我想避免复制+粘贴工作.

I want to avoid the copy+paste work.

目标:在 PyCharm 中为 Python2 输入提示.

Goal: Type hinting in PyCharm for Python2.

推荐答案

就我而言,PyCharm 中没有这样的直接工具.但是,有 3rd 方工具可以做到这一点.

As far as I am concerned, there is no such direct tool in PyCharm. There are, however, 3rd party tools for this.

是的,我想任何想在 Python 中使用编译时类型检查的人,最终都可能使用 MyPy.MyPy 包含 stubgen.py 生成 .pyi 文件的工具.

Yes, I guess anyone who wants to use compile-time type checking in Python, probably ends up using MyPy. MyPy contains stubgen.py tool which generates .pyi files.

mkdir out
stubgen urllib.parse

生成out/urllib/parse.pyi.

你也可以在 Python2 中使用它:

You can use it wit Python2 too:

stubgen --py2 textwrap

对于 C 模块:

scripts/stubgen --docpath <DIR>/Python-3.4.2/Doc/library curses

如果要指定自定义包的路径,可以使用 --search-path 选项:

If you want to specify the path to your custom package, you can use --search-path option:

stubgen my-pkg --search-path=folder/path/to/the/package

make-stub-files

这个项目就是为了这个目标.

make-stub-files

This project is dedicated to exactly this goal.

一个非常基本的(但它有一些选项,只需查阅 README.mdmake_stub_files -h

A very basic one (but it has a handful of options, just consult README.md or make_stub_files -h

make_stub_files foo.py

<小时>

预先编写的.pyi文件

因此您不必自己编写.


pre-written .pyi files

So you don't have to write your own.

是的,如果您在自己的项目中使用 .pyi 文件,您可能想在使用外部代码时也使用它.Typeshed 包含 Python2 和 Python3 stdlib 的 .pyi 文件和一堆 Python2 库(如 redis加密a>, ...) 和一些 Python3 库(如 werkzeugrequests),所有版本都很好.

Yes, if you're using .pyi files in your own project, you probably want to use this also when using external code. Typeshed contains .pyi files for Python2 and Python3 stdlib and a bunch of Python2 libraries (like redis, crypto, ...) and some Python3 libraries (like werkzeug or requests), all nicely versioned.

如果你很懒,或者只是在一个不需要 .pyi 文件的项目上工作,并且你不想被使用 3rd 方工具所困扰,PyCharm 允许你使用:

In case you're lazy, or simply just working on a project which doesn't require .pyi files and you don't want to be bothered by using 3rd party tools, PyCharm allows you to use:

这篇关于自动创建 .pyi 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆