Skip to content

Commit 894dfe0

Browse files
committed
Fixed KeyError when encountering mocked annotations
Fixes #116.
1 parent 5c9210f commit 894dfe0

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
1.10.1
2+
======
3+
4+
* Fixed ``KeyError`` when encountering mocked annotations (``autodoc_mock_imports``)
5+
6+
17
1.10.0
28
======
39

sphinx_autodoc_typehints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def get_annotation_class_name(annotation) -> str:
5858
def get_annotation_args(annotation, module: str, class_name: str) -> Tuple:
5959
try:
6060
original = getattr(sys.modules[module], class_name)
61-
except AttributeError:
61+
except (KeyError, AttributeError):
6262
pass
6363
else:
6464
if annotation is original:

tests/roots/test-dummy/dummy_module.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import typing
2+
from mailbox import Mailbox
23
from typing import Callable, Union
34

45
try:
@@ -248,3 +249,11 @@ class Decorator:
248249

249250
def __init__(self, func: Callable[[int, str], str]):
250251
pass
252+
253+
254+
def mocked_import(x: Mailbox):
255+
"""
256+
A docstring.
257+
258+
:param x: function
259+
"""

tests/roots/test-dummy/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ Dummy Module
3434
:special-members: __init__
3535

3636
.. autodecorator:: dummy_module.Decorator
37+
38+
.. autofunction:: dummy_module.mocked_import

tests/test_sphinx_autodoc_typehints.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def test_sphinx_output(app, status, warning, always_document_param_types):
207207
sys.path.insert(0, str(test_path))
208208

209209
app.config.always_document_param_types = always_document_param_types
210+
app.config.autodoc_mock_imports = ['mailbox']
210211
app.build()
211212

212213
assert 'build succeeded' in status.getvalue() # Build succeeded
@@ -487,6 +488,13 @@ class dummy_module.DataClass
487488
488489
Parameters:
489490
**func** ("Callable"[["int", "str"], "str"]) -- function
491+
492+
dummy_module.mocked_import(x)
493+
494+
A docstring.
495+
496+
Parameters:
497+
**x** ("Mailbox") -- function
490498
''')
491499
expected_contents = expected_contents.format(**format_args).replace('–', '--')
492500
assert text_contents == expected_contents

0 commit comments

Comments
 (0)