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

Fix #9657: autodoc: basecls for a subclass of mocked object is incorrect #9658

Merged
merged 2 commits into from Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -30,6 +30,7 @@ Bugs fixed
* #9651: autodoc: return type field is not generated even if
:confval:`autodoc_typehints_description_target` is set to "documented" when
its info-field-list contains ``:returns:`` field
* #9657: autodoc: The base class for a subclass of mocked object is incorrect
* #9630: autosummary: Failed to build summary table if :confval:`primary_domain`
is not 'py'
* #9670: html: Fix download file with special characters
Expand Down
4 changes: 3 additions & 1 deletion sphinx/ext/autodoc/mock.py
Expand Up @@ -26,6 +26,7 @@ class _MockObject:
"""Used by autodoc_mock_imports."""

__display_name__ = '_MockObject'
__name__ = ''
__sphinx_mock__ = True
__sphinx_decorator_args__: Tuple[Any, ...] = ()

Expand All @@ -40,7 +41,7 @@ def __new__(cls, *args: Any, **kwargs: Any) -> Any:
return super().__new__(cls)

def __init__(self, *args: Any, **kwargs: Any) -> None:
self.__qualname__ = ''
self.__qualname__ = self.__name__

def __len__(self) -> int:
return 0
Expand Down Expand Up @@ -73,6 +74,7 @@ def _make_subclass(name: str, module: str, superclass: Any = _MockObject,
attributes: Any = None, decorator_args: Tuple = ()) -> Any:
attrs = {'__module__': module,
'__display_name__': module + '.' + name,
'__name__': name,
'__sphinx_decorator_args__': decorator_args}
attrs.update(attributes or {})

Expand Down
13 changes: 13 additions & 0 deletions tests/test_util_typing.py
Expand Up @@ -17,6 +17,7 @@

import pytest

from sphinx.ext.autodoc import mock
from sphinx.util.typing import restify, stringify


Expand Down Expand Up @@ -170,6 +171,12 @@ def test_restify_broken_type_hints():
assert restify(BrokenType) == ':py:class:`tests.test_util_typing.BrokenType`'


def test_restify_mock():
with mock(['unknown']):
import unknown
assert restify(unknown.secret.Class) == ':py:class:`unknown.secret.Class`'


def test_stringify():
assert stringify(int) == "int"
assert stringify(str) == "str"
Expand Down Expand Up @@ -294,3 +301,9 @@ def test_stringify_type_union_operator():

def test_stringify_broken_type_hints():
assert stringify(BrokenType) == 'tests.test_util_typing.BrokenType'


def test_stringify_mock():
with mock(['unknown']):
import unknown
assert stringify(unknown.secret.Class) == 'unknown.secret.Class'