![](/img/trans.png)
[英]sphinx and autodocstring from VScode with python code
[英]Can't generate Python docstring with autoDocstring extension in VS Code when multiline string in the function body
要使用 Python Sphinx 生成文档,我必须使用特定的文档字符串格式。
VS Code 扩展autoDocstring能够生成此特定格式,但如果 function 包含多行字符串,则它不起作用。
这种情况下的示例有效:
def func(param1, param2, param3):
# docstring nicely generated
"""_summary_
:param param1: _description_
:type param1: _type_
:param param2: _description_
:type param2: _type_
:param param3: _description_
:type param3: _type_
:return: _description_
:rtype: _type_
"""
random_variable = 42
string_variable = "not a multiline string"
return string_variable
但在这种情况下无法生成自动文档字符串:
def func(param1, param2, param3):
# doesn't work
""""""
random_variable = 42
string_variable = """
a
multiline
string
"""
return string_variable
任何人都知道一个技巧,或者让它起作用的东西? 我在我的函数中使用了很多多行 SQL 字符串,如果我必须提取这些字符串才能使其正常工作,我需要进行大量重构。
键盘快捷键: ctrl+shift+2
或cmd+shift+2
for mac
我想出了解决方案并将其发布在这里,也许会对某人有所帮助。 实际上,解决方案非常简单。 我将函数/类/任何字符串变量中的三重撇号更改为三重单引号,现在 autoDocstring 的解析器不会混淆。 例子:
def func(param1, param2, param3):
# autoDocstring works
"""_summary_
:param param1: _description_
:type param1: _type_
:param param2: _description_
:type param2: _type_
:param param3: _description_
:type param3: _type_
:return: _description_
:rtype: _type_
"""
random_variable = 42
string_variable = '''
a
multiline
string
'''
return string_variable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.