Fix #7812: autosummary: generates broken stub files

This commit is contained in:
Takeshi KOMIYA 2020-06-11 02:51:33 +09:00
parent eff48a9dac
commit d8cf26ef93
3 changed files with 20 additions and 2 deletions

View File

@ -16,6 +16,9 @@ Features added
Bugs fixed Bugs fixed
---------- ----------
* #7812: autosummary: generates broken stub files if the target code contains
an attribute and module that are same name
Testing Testing
-------- --------

View File

@ -615,13 +615,28 @@ def split_full_qualified_name(name: str) -> Tuple[str, str]:
Therefore you need to mock 3rd party modules if needed before Therefore you need to mock 3rd party modules if needed before
calling this function. calling this function.
""" """
from sphinx.util import inspect
parts = name.split('.') parts = name.split('.')
for i, part in enumerate(parts, 1): for i, part in enumerate(parts, 1):
try: try:
modname = ".".join(parts[:i]) modname = ".".join(parts[:i])
import_module(modname) module = import_module(modname)
# check the module has a member named as attrname
#
# Note: This is needed to detect the attribute having the same name
# as the module.
# ref: https://github.com/sphinx-doc/sphinx/issues/7812
attrname = parts[i]
if hasattr(module, attrname):
value = inspect.safe_getattr(module, attrname)
if not inspect.ismodule(value):
return ".".join(parts[:i]), ".".join(parts[i:])
except ImportError: except ImportError:
return ".".join(parts[:i - 1]), ".".join(parts[i - 1:]) return ".".join(parts[:i - 1]), ".".join(parts[i - 1:])
except IndexError:
pass
return name, "" return name, ""

View File

@ -18,7 +18,7 @@ import typing
import warnings import warnings
from functools import partial, partialmethod from functools import partial, partialmethod
from inspect import ( # NOQA from inspect import ( # NOQA
Parameter, isclass, ismethod, ismethoddescriptor Parameter, isclass, ismethod, ismethoddescriptor, ismodule
) )
from io import StringIO from io import StringIO
from typing import Any, Callable, Mapping, List, Optional, Tuple from typing import Any, Callable, Mapping, List, Optional, Tuple