-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 #4956: autodoc: Failed to extract document from a subclass of the class on mocked module #4995
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,14 +23,22 @@ | |
|
||
if False: | ||
# For type annotation | ||
from typing import Any, Callable, Dict, Generator, List, Optional # NOQA | ||
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple # NOQA | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class _MockObject(object): | ||
"""Used by autodoc_mock_imports.""" | ||
|
||
def __new__(cls, *args, **kwargs): | ||
# type: (Any, Any) -> Any | ||
if len(args) == 3 and isinstance(args[1], tuple) and args[1][-1].__class__ is cls: | ||
# subclassing MockObject | ||
return type(args[0], (_MockObject,), args[2], **kwargs) # type: ignore | ||
else: | ||
return super(_MockObject, cls).__new__(cls) | ||
|
||
def __init__(self, *args, **kwargs): | ||
# type: (Any, Any) -> None | ||
pass | ||
|
@@ -47,6 +55,10 @@ def __iter__(self): | |
# type: () -> None | ||
pass | ||
|
||
def __mro_entries__(self, bases): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this method for https://www.python.org/dev/peps/pep-0560/ |
||
# type: (Tuple) -> Tuple | ||
return bases | ||
|
||
def __getitem__(self, key): | ||
# type: (str) -> _MockObject | ||
return self | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
test_ext_autodoc_importer | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Test the autodoc extension. | ||
|
||
:copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS. | ||
:license: BSD, see LICENSE for details. | ||
""" | ||
|
||
from sphinx.ext.autodoc.importer import _MockObject | ||
|
||
|
||
def test_MockObject(): | ||
mock = _MockObject() | ||
assert isinstance(mock.some_attr, _MockObject) | ||
assert isinstance(mock.some_method, _MockObject) | ||
assert isinstance(mock.attr1.attr2, _MockObject) | ||
assert isinstance(mock.attr1.attr2.meth(), _MockObject) | ||
|
||
class SubClass(mock.SomeClass): | ||
"""docstring of SubClass""" | ||
def method(self): | ||
return "string" | ||
|
||
obj = SubClass() | ||
assert SubClass.__doc__ == "docstring of SubClass" | ||
assert isinstance(obj, SubClass) | ||
assert obj.method() == "string" | ||
assert isinstance(obj.other_method(), SubClass) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
confirmed.