PyCharm 2019.2 Help

Specify types with docstrings


You debug your code permanently, and now in the course of debugging you can also collect type information and specify these types in docstrings.

PyCharm provides an intention action that makes it possible to collect type information at runtime, and define type specifications.

However, it is quite possible to specify the types of parameters manually, without the debugger.

Both cases are explored in the section Examples.


Make sure that the checkbox Insert type placeholders in the Smart Keys page of the editor settings is selected.

Note also, that reStructuredText is used for all the subsequent examples, but it is possible to use any of the supported formats of the documentation strings, whether it is plain text, Epytext, Google or NumPy. Refer to the description of the page Python Integrated Tools for details.

Parameter type specification

To specify the parameter types, follow these general steps

  1. Place the caret at the function name, and press Alt+Enter.

  2. In the list of intention actions that opens, choose Insert documentation string stub. PyCharm creates a documentation stub, according to the selected docstring format, with the type specification, collected during the debugger session.


Consider the following code:

import math class SimpleEquation: def demo(self, a, b, c): d = math.sqrt(b ** 2 - 4 * a * c) root1 = (-b + d) / (2 * a) root2 = (-b - d) / (2 * a) print(root1, root2) SimpleEquation().demo(3, 2, 1)

Let us suppose that reStructuredText has been selected as the docstring format on the page Python Integrated Tools.


Place the caret at the name of the function (here it is demo). The suggested intention action is Insert documentation string stub (refer to the section Create documentation comments for details). Click this intention to produce the documentation comment stub in the reStructuredText format:

Docsting example

Then, manually specify the desired types of the parameters:

Docsting example

By the way, you can use quick documentation for the function. If you position the caret at the function name and press Ctrl+Q, you will see:

Docstings in the quick documentation window

Important note about reStructuredText/Sphinx

Note that for reStructuredText it's possible to specify types in two formats:

  • :param param_type param_name: parameter description (type description is on the same line as the parameter description).

  • :type param_name: param_type (type description is on a separate line)

Both variants are shown below:

Using reStructuredText to specify types

With the aid of the debugger

Now, in the Python Debugger page of the Settings/Preferences dialog, select the checkbox Collect runtime information for code insight.

Debug the function call, and use the intention action Insert documentation string stub again. Information about the arguments and return values obtained during the debugging session will be used to pre-populate type annotations in a docstring.

Insert documentation string stub

The obtained result is:

Example of the docstrings for obtaining debugging result
Last modified: 19 September 2019

See Also

Language and Framework-Specific Guidelines: