Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autosummary generation considers NewType instances to be data, but autodoc considers them a class #11552

Open
vector-of-bool opened this issue Aug 4, 2023 · 2 comments

Comments

@vector-of-bool
Copy link

Describe the bug

PR #10700 (addressing #10480) updates autodoc to consider NewType instances to be classes. Within autosummary templates, NewType values are listed in the attributes template parameter, not classes.

Within the rendering of a NewType stub template, the objtype is still data. autosummary documentation suggests that the objtype might be newtypedata, but "newtypedata" does not appear to occur anywhere in the code.

When autodoc generate a function that has a parameter which is a NewType instance, the parameter is annotated with a :py:class: reference, which fails to connect to the autodata entry that was used to generate the NewType stub.

How to Reproduce

Minimal repro: repro.tar.gz

To repro:

$ tar xvf repro.tar.gz
$ cd repro/
$ make

Example output:

$ make                                                                                                                                     23:33:08
sphinx-build . out/ -Wn
Running Sphinx v7.1.2
[autosummary] generating autosummary for: generated/repro.func.rst, generated/repro.rst, index.rst
building [mo]: targets for 0 po files that are out of date
writing output... 
building [html]: targets for 3 source files that are out of date
updating environment: [new config] 3 added, 0 changed, 0 removed
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
copying assets... copying static files... done
copying extra files... done
done
writing output... [100%] index

Warning, treated as error:
.../repro/repro.py:docstring of repro.func:1:py:class reference target not found: repro.NewInt
Makefile:2: recipe for target 'reproduce-error' failed
make: *** [reproduce-error] Error 2

File Contents:

index.rst:

Test
####

.. autosummary::
    :toctree: generated
    :template: module.t.rst

    repro

repro.py:

"""Module"""
import typing

NewInt = typing.NewType("NewInt", int)


def func(v: NewInt) -> None:
    "hey"
    ...

conf.py

import pathlib
import sys

templates_path = ["."]
exclude_patterns = ["*.t.rst"]
sys.path.append(str(pathlib.Path(__file__).parent.resolve()))
extensions = [
    "sphinx.ext.autosummary",
    "sphinx.ext.autodoc",
]

module.t.rst

{{ fullname | escape | underline }}

.. automodule:: {{ fullname }}

  {% if attributes -%}
  .. rubric:: Data
  .. autosummary::
    :toctree:

    {% for data in attributes -%}
    {{ data }}
    {%- endfor %}
  {% endif %}

  {% if functions -%}
  .. rubric:: Functions
  .. autosummary::
    :toctree:

    {% for fn in functions -%}
    {{ fn }}
    {% endfor %}
  {% endif %}

  {% if classes -%}
  .. rubric:: Classes & Types
  .. autosummary::
    :toctree:

    {% for cls in classes -%}
    {{ cls }}
    {% endfor %}
  {% endif %}

Makefile

reproduce-error:
	sphinx-build . out/ -Wn

Environment Information

Please paste all output below into the bug report template



Platform:              linux; (Linux-4.15.0-161-generic-x86_64-with-glibc2.27)
Python version:        3.10.1 (main, Jan 13 2022, 19:20:57) [GCC 7.5.0])
Python implementation: CPython
Sphinx version:        7.1.2
Docutils version:      0.20.1
Jinja2 version:        3.1.2
Pygments version:      2.15.1

Sphinx extensions

sphinx.ext.autodoc
sphinx.ext.autosummary

Additional context

No response

@picnixz
Copy link
Member

picnixz commented Aug 8, 2023

This is due to the analyzer that is detecting them as attributes. Since they are declared the same way a simple variable is declared, the module analyzer cannot determine them to be data-like values.

Btw, I think you need to add some docstring to NewInt otherwise nothing will be generated at all (but I confirm the bug still occurs even with the docstring, it's just that the "target not found" is due to the fact that repro.NewInt.rst was not even generated).

UPDATE: This also happens when autosummarizing a NewType declared inside a class and used outside. However, we do not support local classes (and references to local classes are not created) so I think only the module-level references should be corrected.

@picnixz
Copy link
Member

picnixz commented Aug 8, 2023

I've implemented something: https://github.com/picnixz/sphinx/tree/fix/11552-NewType-autosummary but I need to put the tests (and fix previous ones probably).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants