4

我正在尝试使用 numpy docstring 格式记录元组返回值,但无法使其与 pycharm 类型提示一起使用。

我尝试了多种方法,甚至找到了一种适用于该类型的方法,但不允许我为其每个元素添加描述。

要记录的函数示例:

def function():
    foo = 42
    bar = {
        example : 1337,
        dictionary : 46,
    }
    return foo, bar

现在,我可以记录它的一种方法是:

def function():
    """
    This is the function summary.

    Returns
    -------
    foobar : tuple[int,[dict[string, int]]
        This is a description of the return type
    """
    foo = 42
    bar = {
        'example' : 1337,
        'dictionary' : 46,
    }
    return foo, bar

这会给我一个描述和正确的返回类型提示,但不是我想要的每个元素的单独描述。

这是我要实现的目标的非工作示例:

def function():
    """
    This is the function summary.

    Returns
    -------
    foo : int
        This is an int
    bar : [dict[string, int]
        This is a dictionary
    """
    foo = 42
    bar = {
        'example' : 1337,
        'dictionary' : 46,
    }
    return foo, bar
4

1 回答 1

2

如果function返回值被注释为tuple[int, dict[string, int]],则它的文档会正确呈现,但在推断 的类型时会出现问题function()[1]["key"]。随时在公共 PyCharm 跟踪器https://youtrack.jetbrains.com/issues/PY中提出问题。

于 2019-06-08T12:26:16.463 回答