ast*_*rog 6 python python-sphinx autodoc
根据sphinx文档,该.. autoattribute指令应该能够记录实例属性.但是,如果我这样做::
.. currentmodule:: xml.etree.ElementTree
.. autoclass:: ElementTree
.. autoattribute:: ElementTree._root
Run Code Online (Sandbox Code Playgroud)
然后在构建时我得到一个AttributeError:
Traceback (most recent call last):etree.ElementTree.ElementTree
File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 326, in import_object
obj = self.get_attr(obj, part)
File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 232, in get_attr
return safe_getattr(obj, name, *defargs)
File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/util/inspect.py", line 70, in safe_getattr
raise AttributeError(name)
AttributeError: _root
Run Code Online (Sandbox Code Playgroud)
即使我实例化ElementTree并尝试访问该_root属性,它也可以正常工作::
>>> from xml.etree.ElementTree import ElementTree
>>> e = ElementTree()
>>> hasattr(e, '_root')
True
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
(我实际上有一个我自己的类的问题,但我只是使用ElementTree类作为一个例子,因为它在标准库中)
这看起来像是处理非公共实例属性的方式中的错误。Sphinx 应该能够识别__init__.
我不能说这个应该如何解决。有一个似乎相关的开放错误报告:如果没有 __slots__ ,则不会记录非公共实例属性。
如果将以下行添加到ElementTreeElementTree.py 中类的定义中,
__slots__ = ["_root"]
Run Code Online (Sandbox Code Playgroud)
然后AttributeError你得到的就会消失。