diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-12-29 18:33:36 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-12-29 18:33:36 +0900 |
commit | 3f7bf48715ed5d6b0cf5d2645ee2886975b97b10 (patch) | |
tree | 737b5a11f1285b1bd342ae56ea07be516e2a57f4 /sphinx/ext/autodoc/mock.py | |
parent | b24ae7e206ebcae194a048ac4d980fc7b32d333d (diff) | |
parent | aaee3520f393babd87a34edda48793ed29804365 (diff) | |
download | sphinx-git-3f7bf48715ed5d6b0cf5d2645ee2886975b97b10.tar.gz |
Merge branch '3.4.x' into 3.x
Diffstat (limited to 'sphinx/ext/autodoc/mock.py')
-rw-r--r-- | sphinx/ext/autodoc/mock.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/sphinx/ext/autodoc/mock.py b/sphinx/ext/autodoc/mock.py index 40258a135..3b4991856 100644 --- a/sphinx/ext/autodoc/mock.py +++ b/sphinx/ext/autodoc/mock.py @@ -17,6 +17,7 @@ from types import FunctionType, MethodType, ModuleType from typing import Any, Generator, Iterator, List, Sequence, Tuple, Union from sphinx.util import logging +from sphinx.util.inspect import safe_getattr logger = logging.getLogger(__name__) @@ -147,3 +148,24 @@ def mock(modnames: List[str]) -> Generator[None, None, None]: finally: sys.meta_path.remove(finder) finder.invalidate_caches() + + +def ismock(subject: Any) -> bool: + """Check if the object is mocked.""" + # check the object has '__sphinx_mock__' attribute + if not hasattr(subject, '__sphinx_mock__'): + return False + + # check the object is mocked module + if isinstance(subject, _MockModule): + return True + + try: + # check the object is mocked object + __mro__ = safe_getattr(type(subject), '__mro__', []) + if len(__mro__) > 2 and __mro__[1] is _MockObject: + return True + except AttributeError: + pass + + return False |