简体   繁体   English

Sphinx文档未列出修饰函数的参数

[英]Sphinx docs do not list arguments of decorated functions

When I apply my own decorator (the details are not important, but see below) or other decorators such as cached , from cachetools, my Sphinx-generated docs do not show argument names in their signatures. 当我申请我自己的装饰(细节并不重要,但是见下文)或其他装饰,如cached ,从cachetools,我的狮身人面像,生成的文档没有表现出他们的签名参数名称。

For example, the documentation for 例如,有关

@cached()
def some_func(argA, argB=None):
    ...

@require_unicode('third')
def another_func(first, second=None, third=None):
...

will read, generically 一般会读

some_func(*args, **kwargs) some_func(* args,** kwargs)

another_func(*args, **kwargs) another_func(* args,** kwargs)

rather than, informatively, as 而不是翔实地

some_func(argA, argB=None) some_func(argA,argB = None)

another_func(first, second=None, third=None) another_func(first,second = None,third = None)

How can I remedy this so that my argument names appear in my Sphinx documentation? 我该如何补救,以便使参数名称出现在我的Sphinx文档中? I understand that this is a known issue, and, since I know the names of the decorators I use, I've thought of just making them into no-ops, in my conf.py but can't figure out how do to that. 我知道这是一个已知问题,并且由于我知道我使用的装饰器的名称,因此我想到了在conf.py它们设置为“无操作”,但conf.py该问题。 。

For example, something like this seems promising, but I'm at a loss how to get it working. 例如,像这样很有前途,但我不知所措我如何得到它的工作。 I can put it before the my definition above, but can't see how to get it working for cached . 我可以将其放在上面的我的定义之前,但是看不到如何使它适用于cached


My decorator, for reference. 我的装饰,供参考。 Note that at least this generates docs (which it wouldn't if I didn't use wraps ): 请注意,至少这会生成文档(如果我不使用wraps不会生成文档):

from functools import wraps

def require_unicode(*given_arg_names):
    def check_types(_func_):
        @wraps(_func_)
        def modified(*args, **kwargs):
            arg_names = list(_func_.func_code.co_varnames[:_func_.func_code.co_argcount])
            if len(given_arg_names) == 0:
                raise TypeError('No arguments provided to require_unicode decorator.')
                #unicode_arg_names = arg_names
            else:
                unicode_arg_names = given_arg_names
            for unicode_arg_name in unicode_arg_names:
                try:
                    arg_index = arg_names.index(unicode_arg_name)
                    if len(args) > arg_index:
                        arg = args[arg_index]
                    elif unicode_arg_name in kwargs:
                        arg = kwargs[unicode_arg_name]
                    else:
                    if not isinstance(arg, unicode):
                        raise TypeError("Parameter '{}' should be Unicode".format(unicode_arg_name))
                except ValueError:
                    raise NameError(unicode_arg_name)
            return _func_(*args, **kwargs)
        return modified
    return check_types

Looking at the source code for cached , it doesn't use functools.wraps , so your current monkey patching won't succeed there. 查看cached源代码 ,它不使用functools.wraps ,因此您当前的猴子修补不会在那里成功。 It uses either functools.update_wrapper or its own version, aliased to _update_wrapper , so you'll need to patch out that. 它使用functools.update_wrapper或自己的版本(别名为_update_wrapper ,因此您需要对此进行修补。 If you're using any other libraries with their own ways of implementing wrapping, you'll need to investigate each one accordingly. 如果您使用其他任何具有自己的实现包装方式的库,则需要对每个库进行相应的调查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM