Seb*_*nes 5 python tuples type-hinting pycharm numpydoc
我正在尝试使用 numpy 文档字符串格式记录元组返回值,但无法使其与 pycharm 类型提示一起使用。
我尝试了多种方法,甚至找到了一种适用于该类型的方法,但不允许我为其每个元素添加描述。
要记录的函数示例:
def function():
foo = 42
bar = {
example : 1337,
dictionary : 46,
}
return foo, bar
Run Code Online (Sandbox Code Playgroud)
现在,我可以记录它的一种方法是:
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
Run Code Online (Sandbox Code Playgroud)
这将为我提供描述和正确的返回类型提示,但不是每个元素的单独描述,这是我想要的。
这是我想要实现的目标的一个非工作示例:
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
Run Code Online (Sandbox Code Playgroud)
如果function返回值被注释为tuple[int, dict[string, int]],其文档将正确呈现,但推断 的类型存在问题function()[1]["key"]。请随时在公共 PyCharm 跟踪器https://youtrack.jetbrains.com/issues/PY中提出问题。