【问题标题】:Pycharm docstring: How to add types into docstring with typingPycharm docstring:如何通过键入将类型添加到 docstring 中
【发布时间】:2022-09-27 17:01:51
【问题描述】:

在 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一样的事情吗?

  • 为什么要将类型作为注释和文档字符串进行维护?通常只使用一种或另一种。

标签: python pycharm typing docstring


【解决方案1】:

在 PyCharm 中只有一种半自动的方法可以做到这一点。大多数文档字符串存根可以自动生成,但您需要提供类型。

有一个关于如何做到这一点的官方示例here

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.

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

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多