Skip to content

Commit

Permalink
Move signature option handling into get_items()
Browse files Browse the repository at this point in the history
get_items() already formats the signature. Handling :nosignatures: option in the saves the signature computation cost if the signature is not wanted. Also, this prepares for more complex signature options.
  • Loading branch information
timhoffm committed Dec 6, 2024
1 parent df3d94f commit ec201c2
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions sphinx/ext/autosummary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,15 @@ def create_documenter(self, app: Sphinx, obj: Any,
doccls = get_documenter(app, obj, parent)
return doccls(self.bridge, full_name)

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

items: list[tuple[str, str, str, str]] = []
items: list[tuple[str, str | None, str, str]] = []

max_item_chars = 50

Expand Down Expand Up @@ -365,17 +367,20 @@ def get_items(self, names: list[str]) -> list[tuple[str, str, str, str]]:

# -- Grab the signature

try:
sig = documenter.format_signature(show_annotation=False)
except TypeError:
# the documenter does not support ``show_annotation`` option
sig = documenter.format_signature()

if not sig:
sig = ''
if 'nosignatures' in self.options:
sig = None
else:
max_chars = max(10, max_item_chars - len(display_name))
sig = mangle_signature(sig, max_chars=max_chars)
try:
sig = documenter.format_signature(show_annotation=False)
except TypeError:
# the documenter does not support ``show_annotation`` option
sig = documenter.format_signature()

if not sig:
sig = ''
else:
max_chars = max(10, max_item_chars - len(display_name))
sig = mangle_signature(sig, max_chars=max_chars)

# -- Grab the summary

Expand All @@ -389,7 +394,7 @@ def get_items(self, names: list[str]) -> list[tuple[str, str, str, str]]:

return items

def get_table(self, items: list[tuple[str, str, str, str]]) -> list[Node]:
def get_table(self, items: list[tuple[str, str | None, str, str]]) -> list[Node]:
"""Generate a proper list of table nodes for autosummary:: directive.
*items* is a list produced by :meth:`get_items`.
Expand Down Expand Up @@ -424,10 +429,11 @@ def append_row(*column_texts: str) -> None:

for name, sig, summary, real_name in items:
qualifier = 'obj'
if 'nosignatures' not in self.options:
col1 = f':py:{qualifier}:`{name} <{real_name}>`\\ {rst.escape(sig)}'
else:
if sig is None:
col1 = f':py:{qualifier}:`{name} <{real_name}>`'
else:
col1 = f':py:{qualifier}:`{name} <{real_name}>`\\ {rst.escape(sig)}'

col2 = summary
append_row(col1, col2)

Expand Down

0 comments on commit ec201c2

Please sign in to comment.