sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.41k stars 2.09k forks source link

TypeError: Some type variables (...) are not listed in Generic[...] #12797

Open IkorJefocur opened 3 weeks ago

IkorJefocur commented 3 weeks ago

Describe the bug

Using autodoc extension with autodoc_mock_imports configuration value, importing mocked module, then extending class from that module with generic on a new class causes exception when generating docs. Exception looks like this:

WARNING: [autosummary] failed to import main.
Possible hints:
* KeyError: 'main'
* ValueError: not enough values to unpack (expected 2, got 1)
* TypeError: Some type variables (pydantic.BaseModel) are not listed in Generic[T]

How to Reproduce

# conf.py
project = 'Test'
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.autosummary'
]

autosummary_generate = True
autodoc_mock_imports = ['pydantic']
# index.rst
Documentation
=============

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

   main
# main.py
from pydantic import BaseModel # can be any class from any mocked module

class MyModel[T](BaseModel):
    pass

Environment Information

Platform:              linux; (Linux-6.1.99-1-MANJARO-x86_64-with-glibc2.39)
Python version:        3.12.4 (main, Jun  7 2024, 06:33:07) [GCC 14.1.1 20240522])
Python implementation: CPython
Sphinx version:        8.0.2
Docutils version:      0.21.2
Jinja2 version:        3.1.4
Pygments version:      2.18.0

Sphinx extensions

['sphinx.ext.autodoc', 'sphinx.ext.autosummary']

Additional context

No response

electric-coder commented 3 weeks ago

Possibly related #12512 you might want to take a look at autodoc_pydantic.

IkorJefocur commented 3 weeks ago

@electric-coder It's not a pydantic-related issue, it's more generic. Pydantic here just for example. If I want to inherit any mocked library's class and define any generics on it I will get the same error.

electric-coder commented 3 weeks ago

@IkorJefocur generics have caused a number of problems #11327 I'm lucky not to have run into any of them. As an end-user I'd advise you to try narrowing down the problem because mock, inheritance, generics and pydantic are known to cause problems individually, so I'm not surprised that putting them all together is causing an error. In my experience just because something works at run time it does not mean it won't break 3rd party tooling.

It's not a pydantic-related issue

Can you test it by mocking one of your own moduies and adjust the minimum reproducible example? It would be helpful in narrowing down the issue, the less complexity the better.