首页 > 解决方案 > Sphinx 文档 - 如何隐藏从父类函数继承的函数参数?

问题描述

我有以下课程

import abc


class Parent(metaclass=abc.ABCMeta):
    def __init__(
        self,
        username: str,
        password: str,
        log_file: Optional[str] = None,
        secret: Optional[str] = None,
        local_directory: Optional[str] = None,
        folder_path: Optional[str] = None,
        dataset: Optional[str] = None,
        table: Optional[str] = None,
        columns: Optional[list] = None
    ) -> None:
        """
        :param username: The username or email address of the account used to login to the portal.
        :param password: The password of the account used to login to the portal.
        :param log_file: The log file that all log statements will be stored in. Defaults to stdout.
        :param secret: The secret used to generate valid codes for the account.
        :param local_directory: The working directory where files are saved during runtime.
        :param folder_path: The folder that the reports will be stored in.
        :param dataset: The dataset that will be used to upload the report data to.
        :param table: The table inside ``dataset`` that will house the report data.
        :param columns: The columns as seen in either the downloaded files (recommended) or ``table``.
        """
        pass



class Child(Parent):
    def __init__(
        self,
        username: str,
        password: str,
        section: str,
        context_filter: str = None,
        report_folders: list = None,
        run_fixed_reports_only: bool = False,
        **kwargs
    ) -> None:
        pass

当我尝试为Child下面的类呈现 sphinx 文档时,我最终得到的所有Parent类的参数仍然显示在__init__.

.. autoclass:: Child

   .. automethod:: __init__

Parent除了手动写出每个参数之外,是否有一种简单的方法可以从参数列表中省略' Childs 参数?

编辑:要包括我收到的最终结果的屏幕截图,其中显示了在参数列表之前显示的继承字段Child在此处输入图像描述

标签: pythondocumentationpython-sphinxautodoc

解决方案


现在我只是觉得很傻。原来我所要做的就是在其文档字符串中明确指定Child's__init__参数,如下所示:

class Child(Parent):
    def __init__(
        self,
        username: str,
        password: str,
        portal: str,
        context_filter: str = None,
        report_folders: list = None,
        run_fixed_reports_only: bool = False,
        **kwargs
    ) -> None:
        """
        :param username:
        :param password:
        :param portal:
        :param context_filter:
        :param report_folders:
        :param run_fixed_reports_only:
        :param kwargs: Parameters to pass to the parent class :class:`Parent`.
        """
        pass

推荐阅读