Skip to content

Commit

Permalink
Add ":signatures: short" to autosummary directive
Browse files Browse the repository at this point in the history
This formats functions and classes with (…) if they have
arguments and () if they don't have arguments. This makes
it easier to distinguish callables from attributes and
properties.
  • Loading branch information
timhoffm committed Dec 8, 2024
1 parent 73bb50d commit df273d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion doc/usage/extensions/autosummary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ The :mod:`sphinx.ext.autosummary` extension does this in two parts:
- ``long`` (*default*): use a long signature. This is still cut off so that name
plus signature do not exceeed a certain length.
- ``short``: Function and class signatures are displayed as ``(…)`` if they have
arguments and as ``()`` if they don't have arguments.
- ``none``: do not show signatures.
.. versionadded:: 9.0
.. versionadded:: 8.2
.. rst:directive:option:: nosignatures
Expand Down
16 changes: 9 additions & 7 deletions sphinx/ext/autosummary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:

signatures_option = self.options.get('signatures')
if signatures_option is None:
signatures_option = 'none' if self.options.get('nosignatures') else 'long'
if signatures_option not in ['none', 'long']:
raise ValueError(f"Invalid value for autosummary :signatures: option: "
f"{signatures_option!r}. "
f"Valid values are 'none', 'long'")
signatures_option = 'none' if 'nosignatures' in self.options else 'long'
if signatures_option not in {'none', 'short', 'long'}:
msg = ("Invalid value for autosummary :signatures: option: "
f"{signatures_option!r}. Valid values are 'none', 'short', 'long'")
raise ValueError(msg)

max_item_chars = 50

Expand Down Expand Up @@ -384,10 +384,12 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
except TypeError:
# the documenter does not support ``show_annotation`` option
sig = documenter.format_signature()

if not sig:
sig = ''
else:
elif signatures_option == 'short':
if sig != '()':
sig = '(…)'
else: # signatures_option == 'long'
max_chars = max(10, max_item_chars - len(display_name))
sig = mangle_signature(sig, max_chars=max_chars)

Expand Down

0 comments on commit df273d6

Please sign in to comment.