sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

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

Open IkorJefocur opened this issue 1 year ago • 3 comments

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

IkorJefocur avatar Aug 17 '24 12:08 IkorJefocur

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

electric-coder avatar Aug 17 '24 15:08 electric-coder

@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.

IkorJefocur avatar Aug 17 '24 15:08 IkorJefocur

@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.

electric-coder avatar Aug 17 '24 19:08 electric-coder

I've now met this issue as well.

class MyClass (MockedParent1, MockedParent2, Generic[bli, bla, blub]): ...

Somehow typing things that MockedParent1, MockedParent2 are type variables during Generic.__init_subclass__ and then raises the error as they are not in {bli, bla, blub}. I currently have no clue how to work arround this :/

Well, I tried to better mock MockedParent1/2 with new dummy class, but that failed likely because the mock lookup does not return the dummy object.

Daraan avatar Aug 29 '25 17:08 Daraan