You debug your code permanently, and now in 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.
In this section:
Make sure that the check box Collect run-time types information for code insight in the page Python page of the Debugger settings is selected.
- Open the desired Python class in the editor, and debug its calls.
- Place the caret at any place within a method body, and press Alt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+EnterAlt+Enter, Shift+Alt+J or Ctrl+Shift+MAlt EnterAlt EnterCommand 1.
- In the list of intention actions that opens, choose Generate docstrings with types. 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 list of parameters of the demo function. The suggested intention action is Insert documentation string stub (refer to the section Creating Documentation Comments for details). Click this intention to produce the documentation comment stub in reStructuredText format, and manually specify the desired types of the parameters.
Next, debug the function call. PyCharm collects information about types, and suggests a new intention action Generate docstrings with types. To show this intention action, it is enough to place the caret in any place within the method body.
The obtained result is:
Next, select Epytext format of the docstrings, and repeat the whole procedure. The documentation comment with types includes parameters in Epytext format.