当函数体中存在多行字符串时,无法在 VS Code 中生成带有 autoDocstring 扩展的 Python 文档字符串
要使用 Python Sphinx 生成文档,我必须使用特定的文档字符串格式。
VS Code 扩展 autoDocstring 能够生成这种特定格式,但如果函数包含多行字符串,则它不起作用。
这种情况下的示例有效:
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 字符串,如果我必须提取这些字符串才能使其正常工作,那么我需要进行大量重构。
To generate documentation with Python Sphinx I have to use a specific docstring format.
VS Code extension autoDocstring is capable to generate this specific format, but if the function contains multiline string then it doesn't work.
Example in this case works:
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
But in this case can't generate auto docstring:
def func(param1, param2, param3):
# doesn't work
""""""
random_variable = 42
string_variable = """
a
multiline
string
"""
return string_variable
Anyone know a trick, or something to make it work?
I use a lot of multiline SQL strings in my functions and if I have to extract these strings just to make it work I need a lot of refactoring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
键盘快捷键:
ctrl+shift+2
或cmd+shift+2
(适用于 Mac)Keyboard shortcut:
ctrl+shift+2
orcmd+shift+2
for mac我找到了解决方案,并将其发布在这里,也许会对某人有所帮助。
实际上,解决方案非常简单。
我将函数/类/任何字符串变量中的三个撇号更改为三个单引号,现在 autoDocstring 的解析器不会感到困惑。
例子:
I figured out the solution and I post it here, maybe will help somebody.
Actually the solution is pretty straightforward.
I changed the triple apostrophes to triple single quotes in the function/class/whatever string variable and now autoDocstring's parser doesn't get confused.
Example: