使用带有哨兵对象的默认参数的 Sphinx python 方法进行记录?
如果您希望能够允许人们使用 None
调用某些方法,您必须使用 sentinel 对象。
_sentinel = object()
def foo(param1=_sentinel):
...
这将允许您调用 foo(param1=None)
并能够区分 foo()
等调用。
问题是,当 Sphinx 记录该方法时,它会编写类似“
mymodule.foo(param1=<object object at 0x108c1a520>)
我怎样才能说服 Sphinx 为这些函数提供用户友好的输出?”
之类的内容。请注意,想象一下,如果您使用哨兵方法有 3-4 个参数,文档会是什么样子。
If you want to be able to allow people to call some methods using None
you have to do use a sentinel object when you define the method.
_sentinel = object()
def foo(param1=_sentinel):
...
This would allow you to call foo(param1=None)
and be able to make the difference between a call like foo()
.
The problem is that when Sphinx does document the method it will write something like
mymodule.foo(param1=<object object at 0x108c1a520>)
How can I convince Sphinx to have a user friendly output for these functions?
Note, Imagine how the documentations look if you have 3-4 parameters using the sentinel approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
生成的方法签名的
Sphinx 会将其渲染为如下所示:
The
<object object at 0x108c1a520>
part of generated method signature can be changed by overriding the__repr__
method of the sentinel object.It will be rendered by Sphinx as something like this:
我认为只要你有一个在函数之外创建对象的哨兵,就不可能说服 Sphinx 变得更加“友好”。 Sphinx的autodoc扩展导入模块,这意味着执行模块级代码。
你确定你不能使用这样的东西吗?
I don't think it is possible to persuade Sphinx to be more "friendly" as long as you have a sentinel that creates an object outside the function. Sphinx' autodoc extension imports the module, which means that module-level code is executed.
Are you sure you can't use something like this?
这可以通过在 autodoc 指令中手动指定函数签名来处理,例如:
This can be handled by manually specifying function signature in autodoc directive, e.g.: