Pycharm: Auto generate `:type param:` field in docstring

懵懂的女人 提交于 2019-12-20 18:45:12

问题


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

回答1:


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.




回答2:


In the preference setting:

Smart Keys -> "insert type placeholders in the documentation comment stub"

check this screenshot as reference




回答3:


Just enable this checkbox:

Editor - General - Smart Keys - Insert type placeholders in the documentation comment stub.

Also remember to enable this item so that u can use the Alt+enter to auto insert documentation:

Editor - General - Smart Keys - Insert documentation comment stub




回答4:


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


来源:https://stackoverflow.com/questions/31069526/pycharm-auto-generate-type-param-field-in-docstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!