You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"duplicate object description" warning when Python class has type annotation #8664
A Python class with type annotation produces a warning: WARNING: duplicate object description during the sphinx-build if the sphinx.ext.napoleon extension is used. This used to not produce any warnings on version 1.8.5 of sphinx, but produces the warnings in version 3.4.3.
To Reproduce
Steps to reproduce the behavior:
#!/usr/bin/env bashset -e
set -x
mkdir repro &&cd repro
mkdir -p docs/source
cat <<EOF > generate_warning.pyclass Test: """ Attributes: attr: a doc """ attr: boolEOF
cat <<EOF > docs/source/conf.pyimport osimport syssys.path.insert(0, os.path.abspath('../../'))extensions = [ 'sphinx.ext.napoleon',]source_suffix = '.rst'# The master toctree document.master_doc = 'index'EOF
cat <<EOF > docs/source/index.rst.. toctree:: apidoc/modulesEOF# I use conda to create environemnts, but if you don't create a virtual env with# sphinx 3.4.3 instead of the following block
cat <<EOF > environment.ymlname: sphinx-duplicate-warningchannels: - conda-forge - nodefaultsdependencies: - sphinx==3.4.3EOF
conda env create
Then activate the environment and run sphinx build:
cd repro
conda activate sphinx-duplicate-warning
sphinx-apidoc --force --separate -o docs/source/apidoc .&& sphinx-build -M html "docs/source""build"
Now sphinx-apidoc generates a document for instance variables even not having docstrings (internally, it sets :undoc-members: option by default). Could you set the environment variable SPHINX_APIDOC_OPTIONS=members,show-inheritance before running sphinx-apidoc? Then the entry for Test.attr is not automatically generated.
I have a methodological question -- what was your mental process to figure this out?
It seems in order to understand what was the problem I need to know the stages / sequence of document generation -- is there a document that describes it?
The answer is reading code, reading code, and reading code :-p
In this case, I passed -vv option to sphinx-build commands to investigate what reST code was generated to your example. And I saw the entries for Test.attr are defined twice (one is generated from docstring of Test class, and another one is from type annotated attribute itself).
hi @tk0miya is this a bug in napoleon? What if I want undoc members as well as doc members with type annotations? Shouldn't these 2 parts just merge without warning?
After that, two attributes definition causes a warning on building document.
The napoleon simply converts docstring to the reST document. The autodoc is also. So they works as expected. I believe this is not a bug.
Shouldn't these 2 parts just merge without warning?
Activity
tk0miya commentedon Jan 9, 2021
Now sphinx-apidoc generates a document for instance variables even not having docstrings (internally, it sets
:undoc-members:option by default). Could you set the environment variableSPHINX_APIDOC_OPTIONS=members,show-inheritancebefore running sphinx-apidoc? Then the entry forTest.attris not automatically generated.mezhaka commentedon Jan 9, 2021
@tk0miya Thank you! This works!
I have a methodological question -- what was your mental process to figure this out?
It seems in order to understand what was the problem I need to know the stages / sequence of document generation -- is there a document that describes it?
tk0miya commentedon Jan 10, 2021
The answer is reading code, reading code, and reading code :-p
In this case, I passed
-vvoption tosphinx-buildcommands to investigate what reST code was generated to your example. And I saw the entries forTest.attrare defined twice (one is generated from docstring ofTestclass, and another one is from type annotated attribute itself).ailin-nemui commentedon Apr 26, 2021
hi @tk0miya is this a bug in napoleon? What if I want undoc members as well as doc members with type annotations? Shouldn't these 2 parts just merge without warning?
tk0miya commentedon Apr 26, 2021
I think this might be a limitation of napoleon or autodoc. And there is no workaround for the behavior.
These extensions generates the following document internally from above
Testclass:After that, two attributes definition causes a warning on building document.
The napoleon simply converts docstring to the reST document. The autodoc is also. So they works as expected. I believe this is not a bug.
Please post a PR to resolve it if you find a way!