python - 我可以抑制 Sphinx 文档中的变量扩展吗?

标签 python variables python-sphinx signature autodoc

在我的代码中有

X_DEFAULT = ['a', 'long', 'list', 'of', 'values', 'that', 'is', 'really', 'ugly', 'to', 'see', 'over', 'and', 'over', 'again', 'every', 'time', 'it', 'is', 'referred', 'to', 'in', 'the', 'documentation']

以后

def some_function(..., x=X_DEFAULT, ...):

所以在我的 Sphinx 文档中,使用(例如,使用 .. autofunction:: 等)我得到了 X_DEFAULT 的整个长而笨重的值扩展为some_function 的签名:

some_function(..., x=['a', 'long', 'list', 'of', 'values', 'that', 'is', 'really', 'ugly', 'to', 'see', 'over', 'and', 'over', 'again', 'every', 'time', 'it', 'is', 'referred', 'to', 'in', 'the', 'documentation'], ...)

有没有一种方法可以在生成的文档中抑制这种替换,最好是带有返回到 X_DEFAULT 定义的链接:

some_function(..., x=X_DEFAULT, ...)


我知道我可以手动覆盖我明确列为 Sphinx 文档指令参数的每个函数和方法的签名,但这不是我的目标。我也知道我可以使用 autodoc_docstring_signature和文档字符串的第一行,但这会产生错误的文档字符串,真正用于内省(introspection)失败的情况(如 C)。我怀疑我可以在 autodoc-process-signature 中做些什么这可能就足够了(但并不完美),但我不确定如何继续。

最佳答案

一种方法,例如,“恢复”所有已在模块级别定义为“公共(public)常量”(由全大写名称识别,没有前导下划线)的值的替换,一个唯一的名称可以在定义它的模块中找到:

def pretty_signature(app, what, name, obj, options, signature, return_annotation):
    if what not in ('function', 'method', 'class'):
        return

    if signature is None:
        return

    import inspect
    mod = inspect.getmodule(obj)

    new_sig = signature
    # Get all-caps names with no leading underscore
    global_names = [name for name in dir(mod) if name.isupper() if name[0] != '_']
    # Get only names of variables with distinct values
    names_to_replace = [name for name in global_names
                        if [mod.__dict__[n] for n in global_names].count(mod.__dict__[name]) == 1]
    # Substitute name for value in signature, including quotes in a string value
    for var_name in names_to_replace:
        var_value = mod.__dict__[var_name]
        value_string = str(var_value) if type(var_value) is not str else "'{0}'".format(var_value)
        new_sig = new_sig.replace(value_string, var_name)

    return new_sig, return_annotation

def setup(app):
    app.connect('autodoc-process-signature', pretty_signature)

另一种方法是直接从源代码中获取文档字符串:

import inspect
import re

def pretty_signature(app, what, name, obj, options, signature, return_annotation):
    """Prevent substitution of values for names in signatures by preserving source text."""
    if what not in ('function', 'method', 'class') or signature is None:
        return

    new_sig = signature
    if inspect.isfunction(obj) or inspect.isclass(obj) or inspect.ismethod(obj):
        sig_obj = obj if not inspect.isclass(obj) else obj.__init__
        sig_re = '\((self|cls)?,?\s*(.*?)\)\:'
        new_sig = ' '.join(re.search(sig_re, inspect.getsource(sig_obj), re.S).group(2).replace('\n', '').split())
        new_sig = '(' + new_sig + ')'

    return new_sig, return_annotation

关于python - 我可以抑制 Sphinx 文档中的变量扩展吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21287477/

相关文章:

python-sphinx - 如何从 Sphinx 文档创建到本地 index.html 的链接?

带有 Sphinx 生成的文档的 Github 页面无法正确显示 HTML

python - gTTS 错误 : saving as wav but saved as MPEG

python - 在 Tkinter 标签中嵌入 Matplotlib 图

variables - 数据类型后的问号(?)

Python:使用返回值查找错误原因

python - 如何向 MultiIndex 添加级别?

python - Django中忘记密码的实现

java - 是否可以从另一个类访问变量?

python - 狮身人面像模板