python 继承 超类_Python-Sphinx:“继承”超类的方法文档

Edit:

As of now (Sphinx 1.4.9) there seems to be no way to tell Sphinx to do what I want (see issue on GitHub). The accepted answer from Brecht Machiels solves the problem in an other way, until Sphinx might be able to do so one day.

Description:

I am trying to document a Python project with sphinx-apidoc. The Sphinx config is almost default, I just included 'sphinx.ext.autodoc'.

It works in general, but derived classes do not inherit method documentation of their superclasses as I would expect it.

Example:

Consider a very minimalistic Python package called project. Aside an empty __init__.py it only consists of one file (base.py, see below)

# -*- coding: utf-8 -*

import abc

class Superclass(object):

"""The one to rule them all"""

@abc.abstractmethod

def give(self, ring):

"""Give out a ring"""

pass

class Derived(Superclass):

"""Somebody has to do the work"""

def give(self, ring):

print("I pass the ring {} to you".format(ring))

Running sphinx-apidoc (sphinx-apidoc -o apidoc project -f) generates the following files:

apidoc/modules.rst

project

=======

.. toctree::

:maxdepth: 4

project

apidoc/project.rst

project package

===============

Submodules

----------

project.base module

-------------------

.. automodule:: project.base

:members:

:undoc-members:

:show-inheritance:

Module contents

---------------

.. automodule:: project

:members:

:undoc-members:

:show-inheritance:

Including apidoc/modules.rst in the default index.rst followed by make html generates a basic html documentation for both classes and their methods. Unfortunately, the docstring of Derived.give is empty.

Question:

Is there a way to tell Sphinx to take the parent's method documentation without decorator magic as described in this SO post for every single method?

解决方案

You can manage docstrings automatically by employing a metaclass for the abstract base class. The following is a very basic implementation of such a metaclass. It needs to be extended to properly handle multiple base classes and corner cases.

# -*- coding: utf-8 -*

import abc

class SuperclassMeta(type):

def __new__(mcls, classname, bases, cls_dict):

cls = super().__new__(mcls, classname, bases, cls_dict)

for name, member in cls_dict.items():

if not getattr(member, '__doc__'):

member.__doc__ = getattr(bases[-1], name).__doc__

return cls

class Superclass(object, metaclass=SuperclassMeta):

"""The one to rule them all"""

@abc.abstractmethod

def give(self, ring):

"""Give out a ring"""

pass

class Derived(Superclass):

"""Somebody has to do the work"""

def give(self, ring):

print("I pass the ring {} to you".format(ring))

This is even a better solution than having Sphinx do this, because this will also work when calling help() on the derived classes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值