Skip to content

Commit

Permalink
Fix #7850: autodoc: KeyError is raised for invalid mark up
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Jun 25, 2020
1 parent 3ad8547 commit ef5f0ba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Bugs fixed
----------

* #7844: autodoc: Failed to detect module when relative module name given
* #7850: autodoc: KeyError is raised for invalid mark up when autodoc_typehints
is 'description'

Testing
--------
Expand Down
15 changes: 10 additions & 5 deletions sphinx/ext/autodoc/typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ def merge_typehints(app: Sphinx, domain: str, objtype: str, contentnode: Element
if objtype == 'class' and app.config.autoclass_content not in ('init', 'both'):
return

signature = cast(addnodes.desc_signature, contentnode.parent[0])
if signature['module']:
fullname = '.'.join([signature['module'], signature['fullname']])
else:
fullname = signature['fullname']
try:
signature = cast(addnodes.desc_signature, contentnode.parent[0])
if signature['module']:
fullname = '.'.join([signature['module'], signature['fullname']])
else:
fullname = signature['fullname']
except KeyError:
# signature node does not have valid context info for the target object
return

annotations = app.env.temp_data.get('annotations', {})
if annotations.get(fullname, {}):
field_lists = [n for n in contentnode if isinstance(n, nodes.field_list)]
Expand Down
6 changes: 5 additions & 1 deletion sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ def split_full_qualified_name(name: str) -> Tuple[str, str]:
for i, part in enumerate(parts, 1):
try:
modname = ".".join(parts[:i])
module = import_module(modname)
try:
module = import_module(modname)
except ImportError:
print(parts[i - 1])
module = inspect.safe_getattr(module, parts[i - 1])

# check the module has a member named as attrname
#
Expand Down
8 changes: 8 additions & 0 deletions tests/test_ext_autodoc_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import pytest

from sphinx.testing import restructuredtext

from test_ext_autodoc import do_autodoc

IS_PYPY = platform.python_implementation() == 'PyPy'
Expand Down Expand Up @@ -633,6 +635,12 @@ def test_autodoc_typehints_description(app):
in context)


@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description"})
def test_autodoc_typehints_description_for_invalid_node(app):
text = ".. py:function:: hello; world"
restructuredtext.parse(app, text) # raises no error


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_default_options(app):
Expand Down

0 comments on commit ef5f0ba

Please sign in to comment.