Sphinx autodoc TypeError:' type'对象不可迭代

时间:2015-01-14 00:51:06

标签: python-sphinx autodoc

当尝试构建文档(任何类型:html,手册页,latexpdf ...)时,sphinx在尝试autodoc项目的子目录时失败。我尝试了许多不同的方法来尝试缩小问题的范围,但我似乎无法找到罪魁祸首。所有第一个文件都是由sphinx-apidoc生成的。

目录A包含以下第一个文件:

Directory A
=======================================

Submodules
----------

.. toctree::

   A.fileB
   A.fileC
   A.fileD
   A.fileE

Module contents
---------------

.. automodule:: A
    :members:
    :undoc-members:
    :show-inheritance:

如果删除:members:指令,则不会发生故障。如果文件B或E丢失,或者E中的文件B导入丢失或被注释掉,也不会发生故障。

如果将fileB和fileE配对到以下内容,它仍然会失败:

FILEB:

class B(object):
    pass

fileE:

from fileB import B

class E(B):
    pass

使用以下堆栈跟踪:

# Sphinx version: 1.3b2
# Python version: 2.7.5
# Docutils version: 0.12 release
# Jinja2 version: 2.6
# Last messages:
#   reading sources... [ 37%] foo
#   reading sources... [ 37%] foo
#   reading sources... [ 38%] foo
#   reading sources... [ 38%] foo
#   reading sources... [ 39%] foo
#   reading sources... [ 39%] foo
#   reading sources... [ 40%] foo
#   reading sources... [ 40%] foo
#   reading sources... [ 41%] foo
#   reading sources... [ 41%] problem file
# Loaded extensions:
#   sphinx.ext.autodoc (1.3b2) from Sphinx-1.3b2-py2.7.egg/sphinx/ext/autodoc.pyc
#   sphinx.ext.viewcode (1.3b2) from Sphinx-1.3b2-py2.7.egg/sphinx/ext/viewcode.pyc
Traceback (most recent call last):
  File "Sphinx-1.3b2-py2.7.egg/sphinx/cmdline.py", line 246, in main
    app.build(opts.force_all, filenames)
  File "Sphinx-1.3b2-py2.7.egg/sphinx/application.py", line 257, in build
    self.builder.build_update()
  File "Sphinx-1.3b2-py2.7.egg/sphinx/builders/__init__.py", line 237, in build_update
    'out of date' % len(to_build))
  File "Sphinx-1.3b2-py2.7.egg/sphinx/builders/__init__.py", line 251, in build
    self.doctreedir, self.app))
  File "Sphinx-1.3b2-py2.7.egg/sphinx/environment.py", line 585, in update
    self._read_serial(docnames, app)
  File "Sphinx-1.3b2-py2.7.egg/sphinx/environment.py", line 601, in _read_serial
    self.read_doc(docname, app)
  File "Sphinx-1.3b2-py2.7.egg/sphinx/environment.py", line 753, in read_doc
    pub.publish()
  File "docutils/core.py", line 217, in publish
    self.settings)
  File "docutils/readers/__init__.py", line 72, in read
    self.parse()
  File "docutils/readers/__init__.py", line 78, in parse
    self.parser.parse(self.input, document)
  File "docutils/parsers/rst/__init__.py", line 172, in parse
    self.statemachine.run(inputlines, document, inliner=self.inliner)
  File "docutils/parsers/rst/states.py", line 170, in run
    input_source=document['source'])
  File "docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "docutils/parsers/rst/states.py", line 2726, in underline
    self.section(title, source, style, lineno - 1, messages)
  File "docutils/parsers/rst/states.py", line 327, in section
    self.new_subsection(title, lineno, messages)
  File "docutils/parsers/rst/states.py", line 395, in new_subsection
    node=section_node, match_titles=True)
  File "docutils/parsers/rst/states.py", line 282, in nested_parse
    node=node, match_titles=match_titles)
  File "docutils/parsers/rst/states.py", line 195, in run
    results = StateMachineWS.run(self, input_lines, input_offset)
  File "docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "docutils/parsers/rst/states.py", line 2726, in underline
    self.section(title, source, style, lineno - 1, messages)
  File "docutils/parsers/rst/states.py", line 327, in section
    self.new_subsection(title, lineno, messages)
  File "docutils/parsers/rst/states.py", line 395, in new_subsection
    node=section_node, match_titles=True)
  File "docutils/parsers/rst/states.py", line 282, in nested_parse
    node=node, match_titles=match_titles)
  File "docutils/parsers/rst/states.py", line 195, in run
    results = StateMachineWS.run(self, input_lines, input_offset)
  File "docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "docutils/parsers/rst/states.py", line 2299, in explicit_markup
    nodelist, blank_finish = self.explicit_construct(match)
  File "docutils/parsers/rst/states.py", line 2311, in explicit_construct
    return method(self, expmatch)
  File "docutils/parsers/rst/states.py", line 2054, in directive
    directive_class, match, type_name, option_presets)
  File "docutils/parsers/rst/states.py", line 2103, in run_directive
    result = directive_instance.run()
  File "Sphinx-1.3b2-py2.7.egg/sphinx/ext/autodoc.py", line 1441, in run
    documenter.generate(more_content=self.content)
  File "Sphinx-1.3b2-py2.7.egg/sphinx/ext/autodoc.py", line 816, in generate
    self.document_members(all_members)
  File "Sphinx-1.3b2-py2.7.egg/sphinx/ext/autodoc.py", line 699, in document_members
    members_check_module, members = self.get_object_members(want_all)
  File "Sphinx-1.3b2-py2.7.egg/sphinx/ext/autodoc.py", line 878, in get_object_members
    for mname in memberlist:
TypeError: 'type' object is not iterable

autodoc尝试读取的文件是否有问题,或者autodoc本身存在问题?我可以尝试一些工作吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

因此我的问题的解决方案是A的 init 只是

from A.fileD import D
__all__ == D

什么时候应该包含:

from A.fileD import D
__all__ == ["D"]

Sphinx也已更新,以便在发生这种情况时不会失败,它会发出警告:

WARNING: missing attribute mentioned in :members: or __all__: ...

有关详细信息,请参阅https://github.com/sphinx-doc/sphinx/issues/1674