Sphinx: 类变量"WARNING: py:class引用目标未找到

7 投票
1 回答
3435 浏览
提问于 2025-05-01 09:18

我有两个文件,foo.pybar.py

foo.py 里面的内容是:

import bar


class B():
    a = bar.A

bar.py 里面的内容是:

class A():
    pass

我正在通过 docs/index.rst 来生成这些文件的文档,方法是:

.. automodule:: bar
   :members:
   :undoc-members:

.. automodule:: foo
   :members:
   :undoc-members:

现在,当我用带有挑剔标志(-n)的 build html 命令运行时,我收到了一个警告,内容是 WARNING: py:class reference target not found: A

(env)bash-3.2$ make html
sphinx-build -b html -d _build/doctrees  -n . _build/html
Running Sphinx v1.2.3
loading pickled environment... done
building [html]: targets for 1 source files that are out of date
updating environment: 0 added, 1 changed, 0 removed
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
/Users/caesarbautista/Desktop/test_docs/docs/index.rst:12: WARNING: py:class reference target not found: A
writing additional files... genindex py-modindex search
copying static files... done
copying extra files... done
dumping search index... done
dumping object inventory... done
build succeeded, 1 warning.

Build finished. The HTML pages are in _build/html.

我该如何解决这个警告呢?

到目前为止,我尝试在谷歌和文档中搜索,但都没有找到解决办法。这个问题和 A 的导入方式也没有关系。我试过用 from bar import A,但还是没成功。这个错误信息实在是太难懂了。

我设置的测试项目的副本可以在 这里 找到。

暂无标签

1 个回答

1

在你的代码中,你写的是

class B():
    a = bar.A

你必须使用一个实例化的对象,而不是类的别名。当我把 a = bar.A 替换成 a = bar.A() 时,警告就消失了。

撰写回答