Skip to content

Commit

Permalink
Fix #7812: autosummary: generates broken stub files
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Jun 11, 2020
1 parent eff48a9 commit d83ff08
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Features added
Bugs fixed
----------

* #7812: autosummary: generates broken stub files if the target code contains
an attribute and module that are same name

Testing
--------

Expand Down
16 changes: 15 additions & 1 deletion sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
PycodeError, SphinxParallelError, ExtensionError, FiletypeNotFoundError
)
from sphinx.locale import __
from sphinx.util import inspect
from sphinx.util import logging
from sphinx.util.console import strip_colors, colorize, bold, term_width_line # type: ignore
from sphinx.util.typing import PathMatcher
Expand Down Expand Up @@ -619,9 +620,22 @@ def split_full_qualified_name(name: str) -> Tuple[str, str]:
for i, part in enumerate(parts, 1):
try:
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:
return ".".join(parts[:i - 1]), ".".join(parts[i - 1:])
except IndexError:
pass

return name, ""

Expand Down
2 changes: 1 addition & 1 deletion sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import warnings
from functools import partial, partialmethod
from inspect import ( # NOQA
Parameter, isclass, ismethod, ismethoddescriptor
Parameter, isclass, ismethod, ismethoddescriptor, ismodule
)
from io import StringIO
from typing import Any, Callable, Mapping, List, Optional, Tuple
Expand Down

0 comments on commit d83ff08

Please sign in to comment.