From 16d5f763e744d011574386643ec5f440fd421d5d Mon Sep 17 00:00:00 2001 From: Daraan Date: Sat, 30 Aug 2025 20:01:12 +0200 Subject: [PATCH 1/4] Do not mock __typing_subst__ attribute Co-authored-by: Ikor Jefocur --- sphinx/ext/autodoc/mock.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sphinx/ext/autodoc/mock.py b/sphinx/ext/autodoc/mock.py index 236174a62f9..3a892fcfebd 100644 --- a/sphinx/ext/autodoc/mock.py +++ b/sphinx/ext/autodoc/mock.py @@ -29,6 +29,8 @@ class _MockObject: __name__ = '' __sphinx_mock__ = True __sphinx_decorator_args__: tuple[Any, ...] = () + # Attributes listed here should not be mocked and rather raise an Attribute error: + __sphinx_empty_attrs__: set[str] = {'__typing_subst__'} def __new__(cls, *args: Any, **kwargs: Any) -> Any: # NoQA: ARG004 if len(args) == 3 and isinstance(args[1], tuple): @@ -63,6 +65,8 @@ def __getitem__(self, key: Any) -> _MockObject: return _make_subclass(str(key), self.__display_name__, self.__class__)() def __getattr__(self, key: str) -> _MockObject: + if key in self.__sphinx_empty_attrs__: + raise AttributeError return _make_subclass(key, self.__display_name__, self.__class__)() def __call__(self, *args: Any, **kwargs: Any) -> Any: From 4f2e90a40201f1d40d0488cf487238213459ed46 Mon Sep 17 00:00:00 2001 From: Daraan Date: Sat, 30 Aug 2025 20:05:04 +0200 Subject: [PATCH 2/4] Add tests --- .../test_extensions/test_ext_autodoc_mock.py | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/test_extensions/test_ext_autodoc_mock.py b/tests/test_extensions/test_ext_autodoc_mock.py index b2a0e917bee..a9fe1abdc5f 100644 --- a/tests/test_extensions/test_ext_autodoc_mock.py +++ b/tests/test_extensions/test_ext_autodoc_mock.py @@ -5,7 +5,7 @@ import abc import sys from importlib import import_module -from typing import TypeVar +from typing import TypeVar, Generic import pytest @@ -57,6 +57,28 @@ class SubClass2(mock.SomeClass[T]): assert SubClass2.__doc__ == 'docstring of SubClass' assert isinstance(obj2, SubClass2) + # test subclass with typing.Generic + # Creating this class would raise an error on Python3.11+ + # as mock objects are detected as typevars if hasattr(__typing_subst__) is True. + + assert not hasattr(mock.SomeClass, '__typing_subst__') + S = TypeVar('S') + + class GenericClass(mock.SomeClass, Generic[T, S]): + """docstring of GenericSubclass""" + + obj3 = GenericClass() + assert isinstance(obj3, _MockObject) + assert isinstance(obj3.some_attr, _MockObject) + assert isinstance(obj3.some_method(), _MockObject) + assert isinstance(obj3.attr1.attr2, _MockObject) + assert isinstance(obj3.attr1.attr2.meth(), _MockObject) + + # check that Generic Subscriptions still works + + class GenericSubclass(GenericClass[mock.MockedClass, S]): + """docstring of GenericSubclass""" + def test_mock() -> None: modname = 'sphinx.unknown' From 8eb213234c3fc06337c7902d19bf84fb09281942 Mon Sep 17 00:00:00 2001 From: Ikor Jefocur Date: Fri, 30 Aug 2024 14:09:57 +0300 Subject: [PATCH 3/4] Add changelog --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index b5812ef2f23..80d0bd532ef 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -116,6 +116,9 @@ Bugs fixed * #10785: Autodoc: Allow type aliases defined in the project to be properly cross-referenced when used as type annotations. This makes it possible for objects documented as ``:py:data:`` to be hyperlinked in function signatures. +* #12797: Fix + ``TypeError: Some type variables (...) are not listed in Generic[...]`` + when inheriting from both Generic and autodoc mocked class. Testing ------- From 45d17875b95496e009d046e9b54e304caf3a3838 Mon Sep 17 00:00:00 2001 From: Daniel Sperber Date: Sat, 30 Aug 2025 21:49:09 +0200 Subject: [PATCH 4/4] Order import statements --- tests/test_extensions/test_ext_autodoc_mock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_extensions/test_ext_autodoc_mock.py b/tests/test_extensions/test_ext_autodoc_mock.py index a9fe1abdc5f..0bf6d1ce187 100644 --- a/tests/test_extensions/test_ext_autodoc_mock.py +++ b/tests/test_extensions/test_ext_autodoc_mock.py @@ -5,7 +5,7 @@ import abc import sys from importlib import import_module -from typing import TypeVar, Generic +from typing import Generic, TypeVar import pytest