Pycharm:在docstring中自动生成`:type param:`字段

时间:2015-06-26 09:23:53

标签: python pycharm docstring

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

从那以后:

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

使用generate docstring选项我有(即使 Insert'type'和'rtype'到文档存根启用):

def foo(bar, xyz):
    """


    :param bar:
    :param xyz:
    """
    return bar + xyz

我希望

def foo(bar, xyz):
    """


    :param bar:
    :type bar:
    :param xyz:
    :type xyz:
    """
    return bar + xyz

5 个答案:

答案 0 :(得分:8)

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.

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.

答案 1 :(得分:2)

在首选项设置中:

智能键->“在文档注释存根中插入类型占位符”

check this screenshot as reference

答案 2 :(得分:0)

只需启用此复选框:

  

编辑器 - 常规 - 智能密钥 - 在文档注释存根中插入类型占位符。

还要记得启用此项目以便您可以使用 Alt + 输入来自动插入文档:

  

编辑 - 常规 - 智能密钥 - 插入文档评论存根

答案 3 :(得分:0)

What you asked already has been replied but I find relevant to point that you can use

def foo(bar, xyz):
    """


    :param bar_type bar:
    :param xyz_type xyz:
    """
    return bar + xyz

Use indicate bar_type and xyz_type the types of the variables. A good tip is that you can use | to set more than one possible type. Example:

def foo(bar, xyz):
    """


    :param float|int bar:
    :param numpy.array xyz:
    """
    return bar + xyz

答案 4 :(得分:0)

首先,检查您是否启用了 restructuredText 插件。要检查,请转到首选项 - 插件 - restructuredText(如果未启用,请选中该框以启用它) 接下来,在相同的首选项选项卡中,导航到 Tools > Python Integrated Tools > Docstrings

改变Docstring格式:restructuredText(而不是Plain) 另外,选中复选框

  • 分析文档字符串中的python代码
  • 为 stdlib 渲染外部文档

enter image description here 应用更改并关闭。

最后,要验证更改,请转到功能块并添加三个引号(单引号或双引号)并按 Enter 或空格,您应该会看到自动生成的文档字符串。