【问题标题】:Sphinx decorated classes not documented狮身人面像装饰类未记录
【发布时间】:2015-12-26 00:24:42
【问题描述】:

我正在用 Sphinx 记录我的图书馆。我有装饰师logic_object:

class logic_object:
    """Decorator for logic object class.
    """
    def __init__(self, cls):
        self.cls = cls
        self.__doc__ = self.cls.__doc__

我有gravity 类,由logic_object 装饰:

@logic_object
class gravity:
    """Basic gravity object logic class.

    :param float g: pixels of acceleration
    :param float jf: jump force
    """
#There is more not important code.

我的 Sphinx .rst 文件是:

Mind.Existence
========================
Classes, methods and functions marked with * aren't for usual cases, they are made to help to the rest of the library.

.. automodule:: Mind.Existence
   :members:
   :member-order: bysource

logic_object 使用 autodoc 记录,但 gravity 没有记录。

为什么会发生这种情况以及如何解决?

【问题讨论】:

    标签: python python-sphinx python-decorators autodoc


    【解决方案1】:

    这是因为装饰类不是真正的类对象(不是type 的实例),因此 autodoc 不知道如何记录它。

    要修复它,您必须编写一个自定义文档(例如在您的 conf.py 中):

    from Mind.Existence import logic_object
    from sphinx.ext.autodoc import ClassDocumenter
    
    class MyClassDocumenter(ClassDocumenter):
        objtype = 'logic_object'
        directivetype = 'class'
    
        @classmethod
        def can_document_member(cls, member, membername, isattr, parent):
            return isinstance(member, logic_object)
    
    def setup(app):
        app.add_autodocumenter(MyClassDocumenter)
    

    然后(在您的装饰器中)您还必须从装饰对象中复制 __name____bases__::

    def __init__(self, cls):
        self.cls = cls
        self.__doc__ = cls.__doc__
        self.__name__ = cls.__name__
        self.__bases__ = cls.__bases__
    

    HTH, 卢克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多