Skip to content

Commit 5871ce2

Browse files
authored
Add support for short signatures in autosummary (#13172)
This introduces a new ``:signatures:`` option to ``sphinx.ext.autosummary``. Possible values are: "long" (*default*): same as the current default "short": format functions and classes as ``func()`` if they do not have arguments and ``func(…)`` if they do have arguments. "none": replaces the pre-existing ``:nosignatures:`` (which becomes deprecated) The new functionality here is the "short" option. This allows to distinguish properties and attributes from functions and classes without costing a lot of space for a long signature.
1 parent 3bc10dd commit 5871ce2

File tree

4 files changed

+57
-30
lines changed

4 files changed

+57
-30
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Features added
8080
and include the ``:no-index:`` and ``:no-index-entry:`` options within
8181
:confval:`autodoc_default_options`.
8282
Patch by Jonny Saunders and Adam Turner.
83+
* #13172: Add support for short signatures in autosummary.
84+
Patch by Tim Hoffmann.
8385

8486
Bugs fixed
8587
----------

doc/usage/extensions/autosummary.rst

+17-4
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,31 @@ The :mod:`sphinx.ext.autosummary` extension does this in two parts:
102102
103103
.. versionadded:: 3.1
104104
105-
.. rst:directive:option:: no-signatures
105+
.. rst:directive:option:: signatures: format
106+
107+
How to display signatures. Valid values are
108+
109+
- ``long`` (*default*): use a long signature. This is still cut off so that name
110+
plus signature do not exceeed a certain length.
111+
- ``short``: Function and class signatures are displayed as ``(…)`` if they have
112+
arguments and as ``()`` if they don't have arguments.
113+
- ``none``: do not show signatures.
114+
115+
.. versionadded:: 8.2
116+
117+
.. rst:directive:option:: nosignatures
106118
107119
Do not show function signatures in the summary.
108120
121+
This is equivalent to ``:signatures: none``.
122+
109123
.. versionadded:: 0.6
110124
111125
.. versionchanged:: 8.2
112126
113-
The directive option ``:nosignatures:`` was renamed to ``:no-signatures:``.
127+
The directive option is superseded by the more general ``:signatures: none``.
114128
115-
The previous name has been retained as an alias,
116-
but will be deprecated and removed
129+
It will be deprecated and removed
117130
in a future version of Sphinx.
118131
119132
.. rst:directive:option:: template: filename

sphinx/ext/autosummary/__init__.py

+37-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
The autosummary directive has the form::
1212
1313
.. autosummary::
14-
:no-signatures:
14+
:signatures: none
1515
:toctree: generated/
1616
1717
module.function_1
@@ -237,19 +237,13 @@ class Autosummary(SphinxDirective):
237237
'caption': directives.unchanged_required,
238238
'class': directives.class_option,
239239
'toctree': directives.unchanged,
240-
'no-signatures': directives.flag,
240+
'nosignatures': directives.flag,
241241
'recursive': directives.flag,
242+
'signatures': directives.unchanged,
242243
'template': directives.unchanged,
243-
'nosignatures': directives.flag,
244244
}
245245

246246
def run(self) -> list[Node]:
247-
# Copy the old option name to the new one
248-
# xref RemovedInSphinx90Warning
249-
# deprecate nosignatures in Sphinx 9.0
250-
if 'no-signatures' not in self.options and 'nosignatures' in self.options:
251-
self.options['no-signatures'] = self.options['nosignatures']
252-
253247
self.bridge = DocumenterBridge(
254248
self.env, self.state.document.reporter, Options(), self.lineno, self.state
255249
)
@@ -336,13 +330,25 @@ def create_documenter(
336330
doccls = get_documenter(app, obj, parent)
337331
return doccls(self.bridge, full_name)
338332

339-
def get_items(self, names: list[str]) -> list[tuple[str, str, str, str]]:
333+
def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
340334
"""Try to import the given names, and return a list of
341335
``[(name, signature, summary_string, real_name), ...]``.
336+
337+
signature is already formatted and is None if :nosignatures: option was given.
342338
"""
343339
prefixes = get_import_prefixes_from_env(self.env)
344340

345-
items: list[tuple[str, str, str, str]] = []
341+
items: list[tuple[str, str | None, str, str]] = []
342+
343+
signatures_option = self.options.get('signatures')
344+
if signatures_option is None:
345+
signatures_option = 'none' if 'nosignatures' in self.options else 'long'
346+
if signatures_option not in {'none', 'short', 'long'}:
347+
msg = (
348+
'Invalid value for autosummary :signatures: option: '
349+
f"{signatures_option!r}. Valid values are 'none', 'short', 'long'"
350+
)
351+
raise ValueError(msg)
346352

347353
max_item_chars = 50
348354

@@ -407,17 +413,22 @@ def get_items(self, names: list[str]) -> list[tuple[str, str, str, str]]:
407413

408414
# -- Grab the signature
409415

410-
try:
411-
sig = documenter.format_signature(show_annotation=False)
412-
except TypeError:
413-
# the documenter does not support ``show_annotation`` option
414-
sig = documenter.format_signature()
415-
416-
if not sig:
417-
sig = ''
416+
if signatures_option == 'none':
417+
sig = None
418418
else:
419-
max_chars = max(10, max_item_chars - len(display_name))
420-
sig = mangle_signature(sig, max_chars=max_chars)
419+
try:
420+
sig = documenter.format_signature(show_annotation=False)
421+
except TypeError:
422+
# the documenter does not support ``show_annotation`` option
423+
sig = documenter.format_signature()
424+
if not sig:
425+
sig = ''
426+
elif signatures_option == 'short':
427+
if sig != '()':
428+
sig = '(…)'
429+
else: # signatures_option == 'long'
430+
max_chars = max(10, max_item_chars - len(display_name))
431+
sig = mangle_signature(sig, max_chars=max_chars)
421432

422433
# -- Grab the summary
423434

@@ -431,7 +442,7 @@ def get_items(self, names: list[str]) -> list[tuple[str, str, str, str]]:
431442

432443
return items
433444

434-
def get_table(self, items: list[tuple[str, str, str, str]]) -> list[Node]:
445+
def get_table(self, items: list[tuple[str, str | None, str, str]]) -> list[Node]:
435446
"""Generate a proper list of table nodes for autosummary:: directive.
436447
437448
*items* is a list produced by :meth:`get_items`.
@@ -469,10 +480,11 @@ def append_row(*column_texts: str) -> None:
469480

470481
for name, sig, summary, real_name in items:
471482
qualifier = 'obj'
472-
if 'no-signatures' not in self.options:
473-
col1 = f':py:{qualifier}:`{name} <{real_name}>`\\ {rst.escape(sig)}'
474-
else:
483+
if sig is None:
475484
col1 = f':py:{qualifier}:`{name} <{real_name}>`'
485+
else:
486+
col1 = f':py:{qualifier}:`{name} <{real_name}>`\\ {rst.escape(sig)}'
487+
476488
col2 = summary
477489
append_row(col1, col2)
478490

tests/roots/test-ext-autosummary-ext/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
.. autosummary::
3-
:no-signatures:
3+
:signatures: none
44
:toctree:
55

66
dummy_module

0 commit comments

Comments
 (0)