PyCharm 2025.2 Help

Specify types with docstrings

Introduction

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.

Specify types

Specify parameter types

  1. Press Ctrl+Alt+S and go to Editor | General | Smart Keys | Python.

    Select the Insert type placeholders in the documentation comment stub checkbox.

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

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

Specify return type

You can also document what a function returns. PyCharm will generate a :return: and :rtype: section (or their equivalent in the selected docstring format).

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

  2. In the list of intention actions that opens, choose Specify return type in docstring.

Specify return type in docstring

Example

Consider the following code:

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

Specify types manually

  1. Place the caret at the name of the function (here it is demo). The suggested intention action is Insert documentation string stub. For more information, refer to Create documentation comments.

    insert documentation string stub

    Click this intention to produce the documentation comment stub in the reStructuredText format:

    Docsting example
  2. Manually specify the desired types of the parameters:

    Docstring example

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

    Docstrings in the quick documentation window

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

Specify types with the aid of the debugger

  1. Press Ctrl+Alt+S and go to Build, Execution, Deployment | Python Debugger. In the Debugger page, select the Collect runtime information for code insight checkbox.

  2. Debug the function call. Place the caret at the function name, and press Alt+Enter. In the list of intention actions that opens, choose Insert a documentation string stub.

    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
  3. Inspect the result:

    Specify types with the aid of the debugger
01 September 2025