forked from tox-dev/sphinx-autodoc-typehints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphinx_autodoc_typehints.py
96 lines (74 loc) · 3.27 KB
/
sphinx_autodoc_typehints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import inspect
from sphinx.util.inspect import getargspec
from sphinx.ext.autodoc import formatargspec
try:
from backports.typing import Optional, get_type_hints
except ImportError:
from typing import Optional, get_type_hints
def format_annotation(annotation):
if inspect.isclass(annotation):
if annotation.__module__ == 'builtins':
if annotation.__qualname__ == 'NoneType':
return '``None``'
else:
return ':class:`{}`'.format(annotation.__qualname__)
extra = ''
if annotation.__module__ in ('typing', 'backports.typing'):
if annotation.__qualname__ == 'Union':
params = annotation.__union_params__
if len(params) == 2 and params[1].__qualname__ == 'NoneType':
annotation = Optional
params = (params[0],)
else:
params = getattr(annotation, '__parameters__', None)
if params:
extra = '\\[' + ', '.join(format_annotation(param) for param in params) + ']'
return ':class:`~{}.{}`{}'.format(annotation.__module__, annotation.__qualname__, extra)
return str(annotation)
def process_signature(app, what: str, name: str, obj, options, signature, return_annotation):
if what in ('function', 'method', 'class', 'exception'):
if what in ('class', 'exception'):
obj = getattr(obj, '__init__')
try:
argspec = getargspec(obj)
except TypeError:
return
if what in ('method', 'class'):
if argspec.args:
del argspec.args[0]
return formatargspec(*argspec[:-1]), None
def process_docstring(app, what, name, obj, options, lines):
if what in ('function', 'method', 'class', 'exception'):
if what in ('class', 'exception'):
obj = getattr(obj, '__init__')
# Unwrap until we get to the original definition
while hasattr(obj, '__wrapped__'):
obj = obj.__wrapped__
try:
type_hints = get_type_hints(obj)
except AttributeError:
return
for argname, annotation in type_hints.items():
formatted_annotation = format_annotation(annotation)
if argname == 'return':
insert_index = len(lines)
for i, line in enumerate(lines):
if line.startswith(':rtype:'):
insert_index = None
break
elif line.startswith(':return:'):
insert_index = i
break
elif line.startswith(':param '):
insert_index = i + 1
if insert_index is not None:
lines.insert(insert_index, ':rtype: {}'.format(formatted_annotation))
else:
searchfor = ':param {}:'.format(argname)
for i, line in enumerate(lines):
if line.startswith(searchfor):
lines.insert(i, ':type {}: {}'.format(argname, formatted_annotation))
break
def setup(app):
app.connect('autodoc-process-signature', process_signature)
app.connect('autodoc-process-docstring', process_docstring)