From 4e739b85eeb36b942efed0dff94d4962803ac9ca Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 21 Jun 2020 17:53:04 +0900 Subject: [PATCH] Fix #7850: autodoc: KeyError is raised for invalid mark up --- CHANGES | 2 ++ sphinx/ext/autodoc/typehints.py | 15 ++++++++++----- tests/test_ext_autodoc_configs.py | 8 ++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index f85970ffe..0dcdae502 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,8 @@ Bugs fixed * #7844: autodoc: Failed to detect module when relative module name given * #7856: autodoc: AttributeError is raised when non-class object is given to the autoclass directive +* #7850: autodoc: KeyError is raised for invalid mark up when autodoc_typehints + is 'description' Testing -------- diff --git a/sphinx/ext/autodoc/typehints.py b/sphinx/ext/autodoc/typehints.py index b763bdfc7..4f81a6eae 100644 --- a/sphinx/ext/autodoc/typehints.py +++ b/sphinx/ext/autodoc/typehints.py @@ -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)] diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py index 674620df0..88519b5fd 100644 --- a/tests/test_ext_autodoc_configs.py +++ b/tests/test_ext_autodoc_configs.py @@ -13,6 +13,8 @@ import sys import pytest +from sphinx.testing import restructuredtext + from test_ext_autodoc import do_autodoc IS_PYPY = platform.python_implementation() == 'PyPy' @@ -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):