Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-generate the type of a field in a docstring in PyCharm?

Tags:

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
like image 752
FunkySayu Avatar asked Jun 26 '15 09:06

FunkySayu


People also ask

How do you docstring in PyCharm?

Press Ctrl+Alt+S and go to Editor | General |Smart Keys. Select the Insert type placeholders checkbox in the Smart Keys page of the editor settings. Place the caret at the function name, and press Alt+Enter . In the list of intention actions that opens, choose Insert documentation string stub.

How do you type a docstring?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

How does PyCharm create documentation?

Place the caret somewhere within the function you want to document. Press Alt+Enter to show the available intention actions. PyCharm generates documentation comment stub according to docstring format, selected in the Python Integrated Tools page.


2 Answers

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.

like image 119
jonrsharpe Avatar answered Sep 30 '22 12:09

jonrsharpe


Go to Settings > Editor > General > Smart Keys, then check the box that says Insert type placeholders in the documentation comment stub.

enter image description here

like image 37
PinHao Bill Chen Avatar answered Sep 30 '22 12:09

PinHao Bill Chen