autodoc: only mock specified modules with their descendants

Do not mock the ancestors of the specified modules in
autodoc_mock_imports. Only mock the modules themselves and their
descendants (as specified in the docs).

Fix the test configs accordingly.

Signed-off-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Robin Jarry 2018-01-11 20:53:35 +01:00
parent f3f829664d
commit c24dffc5a8
3 changed files with 14 additions and 14 deletions

View File

@ -23,7 +23,7 @@ from sphinx.util.inspect import isenumclass, safe_getattr
if False:
# For type annotation
from typing import Any, Callable, Dict, Generator, List, Optional, Set # NOQA
from typing import Any, Callable, Dict, Generator, List, Optional # NOQA
logger = logging.getLogger(__name__)
@ -84,13 +84,7 @@ class _MockModule(ModuleType):
class _MockImporter(object):
def __init__(self, names):
# type: (List[str]) -> None
self.base_packages = set() # type: Set[str]
for n in names:
# Convert module names:
# ['a.b.c', 'd.e']
# to a set of base packages:
# set(['a', 'd'])
self.base_packages.add(n.split('.')[0])
self.names = names
self.mocked_modules = [] # type: List[str]
# enable hook by adding itself to meta_path
sys.meta_path = sys.meta_path + [self]
@ -106,9 +100,10 @@ class _MockImporter(object):
def find_module(self, name, path=None):
# type: (str, str) -> Any
base_package = name.split('.')[0]
if base_package in self.base_packages:
return self
# check if name is (or is a descendant of) one of our base_packages
for n in self.names:
if n == name or name.startswith(n + '.'):
return self
return None
def load_module(self, name):

View File

@ -4,6 +4,8 @@ from missing_module import missing_name
import missing_package1.missing_module1
from missing_package2 import missing_module2
from missing_package3.missing_module3 import missing_name
import sphinx.missing_module4
from sphinx.missing_module4 import missing_name2
@missing_name
def decoratedFunction():
@ -16,3 +18,5 @@ class TestAutodoc(object):
def decoratedMethod(self):
"""TestAutodoc::decoratedMethod docstring"""
return None
sphinx.missing_module4.missing_function(len(missing_name2))

View File

@ -69,9 +69,10 @@ extlinks = {'issue': ('http://bugs.python.org/issue%s', 'issue '),
autodoc_mock_imports = [
'missing_module',
'missing_package1.missing_module1',
'missing_package2.missing_module2',
'missing_package3.missing_module3',
'missing_package1',
'missing_package2',
'missing_package3',
'sphinx.missing_module4',
]
# modify tags from conf.py