首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实例属性错误

实例属性错误
EN

Stack Overflow用户
提问于 2015-05-18 20:53:29
回答 1查看 1K关注 0票数 1

我试图使用实例属性来.. autoclass::一个类:

代码语言:javascript
复制
.. autoclass:: synergine.core.Test.Test
   :members:
   :undoc-members:
   :private-members:

of (文件增效/核心/Test.py):

代码语言:javascript
复制
class Test:

    def __init__(self):
        #: A foo bar instance attribute !
        self._foo = 'bar'

但是当我make html狮身人面像引发这个错误时:

代码语言:javascript
复制
/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 。有办法破解吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-15 10:57:13

这个问题仍然存在!当我想创建一个内部开发人员文档时,我遇到了这个问题。因此,我还希望记录私有保护变量和类常量。

示例:

代码语言:javascript
复制
class Foo(object):
    #: foo is a list for ...
    __foo = []
    __bar = None
    """
    bar stores ...
    """
    # [...]

解决方案:

sphinx.util.inspect中更改函数safe_getattr

代码语言:javascript
复制
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

代码语言:javascript
复制
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)    
    # [...]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30312637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档