我试图使用实例属性来.. autoclass::一个类:
.. autoclass:: synergine.core.Test.Test
:members:
:undoc-members:
:private-members:of (文件增效/核心/Test.py):
class Test:
def __init__(self):
#: A foo bar instance attribute !
self._foo = 'bar'但是当我make html狮身人面像引发这个错误时:
/home/bux/Projets/synergine/doc/source/Components.rst:143: WARNING: autodoc: failed to import attribute 'Test._foo' from module 'synergine.core.Test'; the following exception was raised:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/sphinx/util/inspect.py", line 108, in safe_getattr
return getattr(obj, name, *defargs)
AttributeError: type object 'Test' has no attribute '_foo'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/sphinx/ext/autodoc.py", line 342, in import_object
obj = self.get_attr(obj, part)
File "/usr/local/lib/python3.4/dist-packages/sphinx/ext/autodoc.py", line 241, in get_attr
return safe_getattr(obj, name, *defargs)
File "/usr/local/lib/python3.4/dist-packages/sphinx/util/inspect.py", line 114, in safe_getattr
raise AttributeError(name)
AttributeError: _foo为什么?我要做些什么来记录这个实例属性?
编辑:似乎是错误报道: https://github.com/sphinx-doc/sphinx/issues/956 。有办法破解吗?
发布于 2016-01-15 10:57:13
这个问题仍然存在!当我想创建一个内部开发人员文档时,我遇到了这个问题。因此,我还希望记录私有保护变量和类常量。
示例:
class Foo(object):
#: foo is a list for ...
__foo = []
__bar = None
"""
bar stores ...
"""
# [...]解决方案:
在sphinx.util.inspect中更改函数safe_getattr
def safe_getattr(obj, name, *defargs):
"""A getattr() that turns all exceptions into AttributeErrors."""
try:
if name.startswith("__") and not name.endswith("__"): # <-+ these lines
obj_name = obj.__name__ # | have to be
if "_%s%s"%(obj_name,name) in obj.__dict__: # | added
name = "_%s%s"%(obj_name,name) # <-+
return getattr(obj, name, *defargs)
except Exception as error:
# this is a catch-all for all the weird things that some modules do
# with attribute access
if defargs:
return defargs[0]
raise AttributeError(name) 这将消除错误。若要删除生成的文档中受保护的类常量的内部命名空间,请更改sphinx.ext.autodoc中的函数sphinx.ext.autodoc。
def filter_members(self, members, want_all):
# [...]
for (membername, member) in members:
# [...]
elif want_all and membername.startswith('_'):
# ignore members whose name starts with _ by default
keep = self.options.private_members and \
not membername.startswith('_'+namespace) and\ # <- ADD THIS LINE
(has_doc or self.options.undoc_members)
# [...]https://stackoverflow.com/questions/30312637
复制相似问题