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 6, 2024
1 parent 73bb50d commit b87c5bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 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
12 changes: 7 additions & 5 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']:
signatures_option = 'none' if 'nosignatures' in self.options else 'long'
if signatures_option not in ['none', 'short', 'long']:

Check failure on line 324 in sphinx/ext/autosummary/__init__.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR6201)

sphinx/ext/autosummary/__init__.py:324:37: PLR6201 Use a set literal when testing for membership
raise ValueError(f"Invalid value for autosummary :signatures: option: "
f"{signatures_option!r}. "
f"Valid values are 'none', 'long'")
f"Valid values are 'none', 'short', 'long'")

Check failure on line 327 in sphinx/ext/autosummary/__init__.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

sphinx/ext/autosummary/__init__.py:325:30: EM102 Exception must not use an f-string literal, assign to variable first

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 b87c5bc

Please sign in to comment.