Skip to content

Commit 48a345b

Browse files
picnixzAA-Turner
andauthored
Improve type hints for sphinx.ext.autodoc.mock (#12280)
Co-authored-by: Adam Turner <[email protected]>
1 parent 6514a86 commit 48a345b

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ module = [
259259
"sphinx.ext.autodoc",
260260
"sphinx.ext.autodoc.directive",
261261
"sphinx.ext.autodoc.importer",
262-
"sphinx.ext.autodoc.mock",
263-
"sphinx.ext.autodoc.mock",
264262
"sphinx.ext.autosummary.generate",
265263
"sphinx.ext.doctest",
266264
"sphinx.ext.graphviz",

sphinx/ext/autodoc/mock.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
from importlib.abc import Loader, MetaPathFinder
99
from importlib.machinery import ModuleSpec
1010
from types import MethodType, ModuleType
11-
from typing import TYPE_CHECKING, Any
11+
from typing import TYPE_CHECKING
1212

1313
from sphinx.util import logging
1414
from sphinx.util.inspect import isboundmethod, safe_getattr
1515

1616
if TYPE_CHECKING:
1717
from collections.abc import Iterator, Sequence
18+
from typing import Any
1819

1920
logger = logging.getLogger(__name__)
2021

@@ -46,10 +47,10 @@ def __len__(self) -> int:
4647
def __contains__(self, key: str) -> bool:
4748
return False
4849

49-
def __iter__(self) -> Iterator:
50-
return iter([])
50+
def __iter__(self) -> Iterator[Any]:
51+
return iter(())
5152

52-
def __mro_entries__(self, bases: tuple) -> tuple:
53+
def __mro_entries__(self, bases: tuple[Any, ...]) -> tuple[type, ...]:
5354
return (self.__class__,)
5455

5556
def __getitem__(self, key: Any) -> _MockObject:
@@ -68,7 +69,7 @@ def __repr__(self) -> str:
6869

6970

7071
def _make_subclass(name: str, module: str, superclass: Any = _MockObject,
71-
attributes: Any = None, decorator_args: tuple = ()) -> Any:
72+
attributes: Any = None, decorator_args: tuple[Any, ...] = ()) -> Any:
7273
attrs = {'__module__': module,
7374
'__display_name__': module + '.' + name,
7475
'__name__': name,
@@ -144,8 +145,8 @@ def mock(modnames: list[str]) -> Iterator[None]:
144145
# mock modules are enabled here
145146
...
146147
"""
148+
finder = MockFinder(modnames)
147149
try:
148-
finder = MockFinder(modnames)
149150
sys.meta_path.insert(0, finder)
150151
yield
151152
finally:

tests/test_extensions/test_ext_autodoc_automodule.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
source file translated by test_build.
55
"""
66

7+
import inspect
78
import sys
9+
import typing
810

911
import pytest
1012

@@ -185,8 +187,22 @@ def test_automodule_inherited_members(app):
185187
'sphinx.missing_module4']})
186188
@pytest.mark.usefixtures("rollback_sysmodules")
187189
def test_subclass_of_mocked_object(app):
190+
from sphinx.ext.autodoc.mock import _MockObject
188191
sys.modules.pop('target', None) # unload target module to clear the module cache
189192

193+
options = {'members': None}
194+
actual = do_autodoc(app, 'module', 'target.need_mocks', options)
195+
# ``typing.Any`` is not available at runtime on ``_MockObject.__new__``
196+
assert '.. py:class:: Inherited(*args: Any, **kwargs: Any)' in actual
197+
198+
# make ``typing.Any`` available at runtime on ``_MockObject.__new__``
199+
sig = inspect.signature(_MockObject.__new__)
200+
parameters = sig.parameters.copy()
201+
for name in ('args', 'kwargs'):
202+
parameters[name] = parameters[name].replace(annotation=typing.Any)
203+
sig = sig.replace(parameters=tuple(parameters.values()))
204+
_MockObject.__new__.__signature__ = sig # type: ignore[attr-defined]
205+
190206
options = {'members': None}
191207
actual = do_autodoc(app, 'module', 'target.need_mocks', options)
192208
assert '.. py:class:: Inherited(*args: ~typing.Any, **kwargs: ~typing.Any)' in actual

0 commit comments

Comments
 (0)