-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathattr_types.py
More file actions
52 lines (39 loc) · 1.51 KB
/
attr_types.py
File metadata and controls
52 lines (39 loc) · 1.51 KB
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
import sys
import attr
def add_attr_types(app, what, name, obj, options, lines):
# If we are generating docs for an attr.ib-defined attribute
# where 'type' has been set, append this type info to the
# doc string in the format understood by sphinx.
if what != "attribute":
# not an attribute => nothing to do
return
if ":type:" in "".join(lines):
# type has already been documented explicitly, don't
# try to override it
return
components = name.split(".")
if len(components) != 3 or components[0] != "pushsource":
# We are looking specifically for public fields of the form:
# pushsource.<class_name>.<attr_name>
# For any other cases we'll do nothing.
return
klass_name, field_name = components[1:]
klass = getattr(sys.modules["pushsource"], klass_name)
if not attr.has(klass):
# not an attrs-using class, nothing to do
return
field = attr.fields_dict(klass).get(field_name)
if not field:
# not an attr.ib
return
type = field.type
if not type:
# no type hint declared, can't do anything
return
# phew, after all the above we know we're documenting an attrs-based
# field and we know exactly what type it is, so add it to the end of
# the doc string.
lines.extend(["", ":type: %s" % type.__name__])
def setup(app):
# entrypoint invoked by sphinx when extension is loaded
app.connect("autodoc-process-docstring", add_attr_types)