如何在 PyCharm 的文档字符串中自动生成字段的类型? [英] How to auto-generate the type of a field in a docstring in PyCharm?

查看:88
本文介绍了如何在 PyCharm 的文档字符串中自动生成字段的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个带参数的函数时,PyCharm 让我用 :param param_name: 字段创建文档字符串,这非常好.但我还需要添加 :type param_name:.

因此:

def foo(bar, xyz):返回栏 + xyz

有了生成文档字符串选项,我就有了(即使在Insert 'type' 和 'rtype' 到文档存根 启用):

def foo(bar, xyz):""":参数栏::param xyz:"""返回栏 + xyz

而且我愿意:

def foo(bar, xyz):""":参数栏::类型栏::param xyz::type xyz:"""返回栏 + xyz

解决方案

Per 文档:

<块引用>

如果已配置,文档可以生成注释存根带有 typertype 标签.

点击链接:

<块引用>

...

  1. 智能钥匙页面中,选中复选框<强>插入类型"和'rtype' 到文档注释存根.

<小时>

请注意,文档已更新,当前配置指南为:

<块引用>

启用文档注释

  1. 打开编辑器 |一般 |PyCharm 设置的 Smart Keys 页面 ⌃⌥S.

  2. 回车部分,选中或清除插入文档注释存根复选框.

  3. 然后,滚动到在文档注释存根选项中插入类型占位符并选中或清除复选框按要求.详情请参阅选项说明.

<小时>

完成此操作后,将光标置于定义中的参数名称中,激活智能键功能(Alt+Enter,默认情况下)并选择 在文档字符串中指定引用类型.这将插入适当的注释行.同样,您可以将光标放在函数/方法名称中,然后选择在文档字符串中指定返回类型.

When I create a function with parameters, PyCharm offers me to create the docstring with :param param_name: field, which is pretty good. But I also need to add the :type param_name:.

So from that :

def foo(bar, xyz):
    return bar + xyz

With the generate docstring option i have that (even with Insert 'type' and 'rtype' to the documentation stub enable) :

def foo(bar, xyz):
    """
    :param bar:
    :param xyz:
    """
    return bar + xyz

And I would like that :

def foo(bar, xyz):
    """
    :param bar:
    :type bar:
    :param xyz:
    :type xyz:
    """
    return bar + xyz

解决方案

Per the documentation:

If configured, the documentation comment stubs can be generated with type and rtype tags.

Following the link:

...

  1. In the Smart Keys page, select the check box Insert 'type' and 'rtype' to the documentation comment stub.


Note that the documentation has since been updated, the configuration guidance currently reads:

Enable documentation comments

  1. Open the Editor | General | Smart Keys page of PyCharm settings ⌃⌥S.

  2. In the Enter section, select or clear Insert documentation comment stub checkbox.

  3. Then, scroll to the Insert type placeholders in the documentation comment stub option and select or clear the checkbox as required. Refer to the option description for details.


Once you have done this, put the cursor in a parameter name in the definition, activate the Smart Keys feature (Alt+Enter, by default) and select Specify type for reference in docstring. This will insert the appropriate comment line . Similarly you can put the cursor in the function/method name and select Specify return type in docstring.

这篇关于如何在 PyCharm 的文档字符串中自动生成字段的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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