Python autodoc和显式指定的实例属性存在问题
我编写了一个python类,并用sphinx编写了文档。例如,该类看起来像:Python autodoc和显式指定的实例属性存在问题,python,python-sphinx,autodoc,Python,Python Sphinx,Autodoc,我编写了一个python类,并用sphinx编写了文档。例如,该类看起来像: class Aclass(object): """ my class """ def __init__(self): """ constructor """ self.a = None """ doc for attribute a """ self._prop = None def _get_prop(self):
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
现在,为了编写文档,我在rst文件中写道:
.. autoclass:: aclass.Aclass
:members:
它的所有属性都正常,a
,prop
和square
出现在文档中
但如果我试图分别记录属性和方法,sphinx说它找不到属性a,但它对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
我在某个地方读到sphinx不表示类,因此类属性(作为prop)和实例属性(作为a)之间存在差异。但如何引用文档中的实例属性
实际上,如果在rst文件中没有明确要求实例属性,则会找到它们。例如,这将起作用:
.. autoclass:: aclass.Aclass
:members:
但事实并非如此
.. autoclass: aclass.Aclass
:members: a
有一个关于此的错误报告(创建于2012年3月30日;2015年12月12日仍处于打开状态):
:members:
列表的问题已在中修复(包括在Sphinx 1.2b1中)
autoinstanceattribute
指令
使用。。autoinstanceattribute::aclass.aclass.a
有效(使用Sphinx 1.1.3和1.2b1进行测试)
S.Lott的解决方案提供了一个额外的解决方案:看起来
autoinstanceattribute
不再有效:我有警告:未知指令类型“autoinstanceattribute”。
@GordonBai:bug报告于2020年7月16日关闭。autoattribute
有效吗?有人提到autoattribute
指令会为实例属性引发一个AttributeError
。然后,评论员用一句话解决了这个问题。但是,在我的测试中,它仍然会引发AttributeError
。我用的是斯芬克斯4.0.2。