summaryrefslogtreecommitdiff
path: root/sphinx/ext/autodoc/mock.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-12-29 18:33:36 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-12-29 18:33:36 +0900
commit3f7bf48715ed5d6b0cf5d2645ee2886975b97b10 (patch)
tree737b5a11f1285b1bd342ae56ea07be516e2a57f4 /sphinx/ext/autodoc/mock.py
parentb24ae7e206ebcae194a048ac4d980fc7b32d333d (diff)
parentaaee3520f393babd87a34edda48793ed29804365 (diff)
downloadsphinx-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.py22
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