Pycharm-docstring:如何通过键入将类型添加到docstring中



在vscode中,我安装了autoDocstring插件,它可以生成基于Typing类型的文档。如下所示:

def split_dataset(
data: torch_geometric.data.Data,
train: float = 0.1,
test: float = 0.8,
val: float = 0.1,
) -> tuple:
"""_summary_
Parameters
----------
data : torch_geometric.data.Data
_description_
train : float, optional
_description_, by default 0.1
test : float, optional
_description_, by default 0.8
val : float, optional
_description_, by default 0.1
Returns
-------
tuple
_description_
"""
pass

但在pycharm中,我尝试使用docstring,它不会自动生成类型,如下所示:

def split_dataset(
data: torch_geometric.data.Data,
train: float = 0.1,
test: float = 0.8,
val: float = 0.1,
) -> tuple:
"""

Parameters
----------
data : 
train : 
test : 
val : 
Returns
-------
"""
pass

有没有办法让pycharm做与vscode相同的事情?

PyCharm中只有一种半自动的方法。大多数docstring存根都可以自动生成,但您需要提供类型。

这里有一个关于如何做到这一点的官方例子

TL;博士

1. Press ⌘ + , and go to Editor | General | Smart Keys.
2. Select the Insert type placeholders checkbox in the Smart Keys page of the editor settings.
3. Place the caret at the function name, and press ⌥ ⏎.
4. 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.

然后,手动指定所需的类型。

最新更新