20
20
21
21
Once either `sphinx issue 4826`_ or `sphinx-autodoc-typehints issue 38`_ are fixed,
22
22
this part of the functionality will no longer be necessary.
23
+ #. The config value ``annotate_defaults`` (default: :data:`True`) controls if rST code
24
+ like ``(default: `42`)`` is added after the type.
23
25
24
26
.. _sphinx issue 4826: https://github.com/sphinx-doc/sphinx/issues/4826
25
27
.. _sphinx-autodoc-typehints issue 38: https://github.com/agronholm/sphinx-autodoc-typehints/issues/38
60
62
"scipy.sparse.csc.csc_matrix" : "scipy.sparse.csc_matrix" ,
61
63
}
62
64
qualname_overrides = ChainMap ({}, qualname_overrides_default )
65
+ annotate_defaults = True
63
66
64
67
65
68
def _init_vars (app : Sphinx , config : Config ):
69
+ global annotate_defaults
66
70
qualname_overrides .update (config .qualname_overrides )
71
+ annotate_defaults = config .annotate_defaults
67
72
config .html_static_path .append (str (HERE / "static" ))
68
73
69
74
@@ -119,13 +124,15 @@ def _format_terse(annotation: Type[Any], fully_qualified: bool = False) -> str:
119
124
120
125
121
126
def format_annotation (annotation : Type [Any ], fully_qualified : bool = False ) -> str :
122
- """Generate reStructuredText containing links to the types.
127
+ r """Generate reStructuredText containing links to the types.
123
128
124
129
Unlike :func:`sphinx_autodoc_typehints.format_annotation`,
125
130
it tries to achieve a simpler style as seen in numeric packages like numpy.
126
131
127
132
Args:
128
133
annotation: A type or class used as type annotation.
134
+ fully_qualified: If links should be formatted as fully qualified
135
+ (e.g. ``:py:class:`foo.Bar```) or not (e.g. ``:py:class:`~foo.Bar```).
129
136
130
137
Returns:
131
138
reStructuredText describing the type
@@ -139,10 +146,18 @@ def format_annotation(annotation: Type[Any], fully_qualified: bool = False) -> s
139
146
curframe = inspect .currentframe ()
140
147
calframe = inspect .getouterframes (curframe , 2 )
141
148
if calframe [1 ][3 ] == "process_docstring" :
142
- return (
149
+ annot_fmt = (
143
150
f":annotation-terse:`{ _escape (_format_terse (annotation , fully_qualified ))} `\\ "
144
151
f":annotation-full:`{ _escape (_format_full (annotation , fully_qualified ))} `"
145
152
)
153
+ if annotate_defaults :
154
+ variables = calframe [1 ].frame .f_locals
155
+ sig = inspect .signature (variables ["obj" ])
156
+ if variables ["argname" ] != "return" :
157
+ default = sig .parameters [variables ["argname" ]].default
158
+ if default is not inspect .Parameter .empty :
159
+ annot_fmt += f" (default: ``{ _escape (repr (default ))} ``)"
160
+ return annot_fmt
146
161
else : # recursive use
147
162
return _format_full (annotation , fully_qualified )
148
163
@@ -184,7 +199,10 @@ def _unescape(rst: str) -> str:
184
199
@_setup_sig
185
200
def setup (app : Sphinx ) -> Dict [str , Any ]:
186
201
"""Patches :mod:`sphinx_autodoc_typehints` for a more elegant display."""
187
- app .add_config_value ("qualname_overrides" , {}, "" )
202
+ # TODO: Unsure if “html” is sufficient or if we need to do “env”;
203
+ # Depends on when the autodoc-process-docstring event is handled.
204
+ app .add_config_value ("qualname_overrides" , {}, "html" )
205
+ app .add_config_value ("annotate_defaults" , True , "html" )
188
206
app .add_css_file ("typehints.css" )
189
207
app .connect ("config-inited" , _init_vars )
190
208
sphinx_autodoc_typehints .format_annotation = format_annotation
0 commit comments