为什么我无法使用Sphinx Python软件包打印出docstring?

发布于 2025-01-22 13:14:13 字数 1286 浏览 2 评论 0原文

我正在使用狮身人面像记录我的功能。但是,它没有阅读我的Doc字符串,以便使用装饰器读取功能。 sphinx只需用<魔术替换函数doc字符串。魔术在这里发生>

我的功能是用装饰器以这种方式编写的。如何使狮身人面像检测功能中的DOC字符串。

文件夹处于此格式

  • 项目
    • 功能 - function.py
      • 文档
def wrap(pre, post):
    """ Wrapper """

    def decorate(func):
        """ Decorator """

        def call(*args, **kwargs):
            """ The magic happens here """
            pre(func)
            result = func(*args, **kwargs)
            post(func)
            return result
        return call
    return decorate

def entering(func, *args):
    """ Pre function logging """
    logging.debug("Entered {}".format(func.__name__))
    # logging.info(func.__doc__)
    logging.debug("Function at line {} in {}".format(func.__code__.co_firstlineno, func.__code__.co_filename))
    try:
        logging.debug("The argument {} is {}".format(func.__code__.co_varnames[0], *args))
    except IndexError:
        logging.debug("No arguments")


def exiting(func):
    """ Post function logging """
    logging.debug("Exited {}".format(func.__name__))

@wrap(entering, exiting)
def function(a, b):
    """
    Function to execute
    :param a:
    :param b:
    :return: Sum of a+b
    """
    return a+b

I am using Sphinx to document out my functions. However, it does not read my doc strings for function with a decorator.
Sphinx just replace the function doc strings with <The magic happens here>

My functions are written in this manner with a decorator. How do i make Sphinx detect the doc strings in the function.

Folder is in this format

  • Project
    • Functions
      - function.py

      • docs
def wrap(pre, post):
    """ Wrapper """

    def decorate(func):
        """ Decorator """

        def call(*args, **kwargs):
            """ The magic happens here """
            pre(func)
            result = func(*args, **kwargs)
            post(func)
            return result
        return call
    return decorate

def entering(func, *args):
    """ Pre function logging """
    logging.debug("Entered {}".format(func.__name__))
    # logging.info(func.__doc__)
    logging.debug("Function at line {} in {}".format(func.__code__.co_firstlineno, func.__code__.co_filename))
    try:
        logging.debug("The argument {} is {}".format(func.__code__.co_varnames[0], *args))
    except IndexError:
        logging.debug("No arguments")


def exiting(func):
    """ Post function logging """
    logging.debug("Exited {}".format(func.__name__))

@wrap(entering, exiting)
def function(a, b):
    """
    Function to execute
    :param a:
    :param b:
    :return: Sum of a+b
    """
    return a+b

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2025-01-29 13:14:13

您需要装饰函数才能将docstring从func将其复制到调用之前。否则,Sphinx只能获取呼叫的DocString,而不是原始功能的docstring。

您可以通过直接执行致电。__doc__ = func .__ doc __,也可以使用function> function> wrapss.wrapss.wraps decortor 默认情况下还要复制其他一些属性,并且可以进行很多定制)。

我会尝试:

import functools

def wrap(pre, post):
    """ Wrapper """

    def decorate(func):
        """ Decorator """

        @functools.wraps(func)              # this copies over the docstring and more
        def call(*args, **kwargs):
            """ The magic happens here """
            pre(func)
            result = func(*args, **kwargs)
            post(func)
            return result
        return call
    return decorate

You need your decorate function to copy the docstring from func onto call before returning it. Otherwise Sphinx can only get the docstring of call, not the docstring of the original function.

You can do this yourself by directly doing call.__doc__ = func.__doc__, or you could use the functools.wraps decorator from the standard library to do it for you (which also copies a few other attributes by default, and can be customized quite a bit).

I'd try:

import functools

def wrap(pre, post):
    """ Wrapper """

    def decorate(func):
        """ Decorator """

        @functools.wraps(func)              # this copies over the docstring and more
        def call(*args, **kwargs):
            """ The magic happens here """
            pre(func)
            result = func(*args, **kwargs)
            post(func)
            return result
        return call
    return decorate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文