本文介绍了autodoc和明确指定的实例属性存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个python类,并使用sphinx编写了文档。例如,该类如下所示:

I wrote a python class and I made the documentation with sphinx. For example, the class looks like :

class Aclass(object):
    """ my class """

    def __init__(self):
        """ constructor """

        self.a = None
        """ doc for attribute a """

        self._prop = None

    def _get_prop(self):
        """ getter prop """
        return self._prop

    def _set_prop(self, val):
        """ setter prop """
        self._prop = val

    prop = property(_get_prop, _set_prop)
    """ a property """

    def square(self):
        """ return square of a """
        return self.a**2

现在,为了做文档,在我写的第一个文件中:

Now, in order to do the documentation, in the rst file I wrote :

.. autoclass:: aclass.Aclass
   :members:

一切正常, a prop 并且 square 出现在文档中。

All its ok, and a, prop and square appears in the doc.

但是如果我尝试记录属性和方法分开使用,狮身人面像说它无法找到属性a,但适用于prop。

But If I try to document attributes and methods separatly, sphinx says that it cannont find attribute a but it works for prop.

.. autoattribute:: aclass.Aclass.prop

.. autoattribute:: aclass.Aclass.a

错误消息是:

Traceback (most recent call last):                                                                                
  File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 326, in import_object
    obj = self.get_attr(obj, part)
  File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 232, in get_attr
    return safe_getattr(obj, name, *defargs)
  File "/usr/lib/python2.7/dist-packages/sphinx/util/inspect.py", line 70, in safe_getattr
    raise AttributeError(name)
AttributeError: a

/home/gvallver/dev/sphinx/doc/source/index.rst:17: WARNING: autodoc can't import/find attribute 'aclass.Aclass.a', it reported error: "a", please check your spelling and sys.path

我在某处阅读。

There is a bug report about this (created 2012-03-30; still open 2015-12-12): https://github.com/sphinx-doc/sphinx/issues/904.


  1. 具有显式:members:包含实例属性的列表的问题已在(包含在Sphinx 1.2b1中)。

  1. The problem with an explicit :members: list containing instance attributes was fixed in this commit (included in Sphinx 1.2b1).

注释(来自Jon Waltman),其中有一个未记录的 autoinstanceattribute 指令。

As mentioned in a comment (from Jon Waltman), there is an undocumented autoinstanceattribute directive.

使用。 。autoinstanceattribute :: aclass.Aclass.a 确实有效(使用Sphinx 1.1.3和1.2b1测试)。

Using .. autoinstanceattribute:: aclass.Aclass.a does work (tested with Sphinx 1.1.3 and 1.2b1).

这篇关于autodoc和明确指定的实例属性存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 02:15