Skip to content

Commit a3f08fe

Browse files
authored
Merge pull request #9606 from tk0miya/9600_typehints_including_commas
Fix #9600: autosummary: Typehints including commas confuses autosummary
2 parents 4ad45ce + 0a8655b commit a3f08fe

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Bugs fixed
3333
are not displayed well
3434
* #9481: autosummary: some warnings contain non-existing filenames
3535
* #9568: autosummary: summarise overlined sectioned headings correctly
36+
* #9600: autosummary: Type annotations which contain commas in autosummary table
37+
are not removed completely
3638
* #9481: c domain: some warnings contain non-existing filenames
3739
* #9481: cpp domain: some warnings contain non-existing filenames
3840
* #9456: html search: abbreation marks are inserted to the search result if

sphinx/ext/autosummary/__init__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import re
5959
import sys
6060
import warnings
61+
from inspect import Parameter
6162
from os import path
6263
from types import ModuleType
6364
from typing import Any, Dict, List, Optional, Tuple, Type, cast
@@ -87,6 +88,7 @@
8788
from sphinx.util import logging, rst
8889
from sphinx.util.docutils import (NullReporter, SphinxDirective, SphinxRole, new_document,
8990
switch_source_input)
91+
from sphinx.util.inspect import signature_from_str
9092
from sphinx.util.matching import Matcher
9193
from sphinx.util.typing import OptionSpec
9294
from sphinx.writers.html import HTMLTranslator
@@ -456,10 +458,32 @@ def strip_arg_typehint(s: str) -> str:
456458
return s.split(':')[0].strip()
457459

458460

461+
def _cleanup_signature(s: str) -> str:
462+
"""Clean up signature using inspect.signautre() for mangle_signature()"""
463+
try:
464+
sig = signature_from_str(s)
465+
parameters = list(sig.parameters.values())
466+
for i, param in enumerate(parameters):
467+
if param.annotation is not Parameter.empty:
468+
# Remove typehints
469+
param = param.replace(annotation=Parameter.empty)
470+
if param.default is not Parameter.empty:
471+
# Replace default value by "None"
472+
param = param.replace(default=None)
473+
parameters[i] = param
474+
sig = sig.replace(parameters=parameters, return_annotation=Parameter.empty)
475+
return str(sig)
476+
except Exception:
477+
# Return the original signature string if failed to clean (ex. parsing error)
478+
return s
479+
480+
459481
def mangle_signature(sig: str, max_chars: int = 30) -> str:
460482
"""Reformat a function signature to a more compact form."""
483+
s = _cleanup_signature(sig)
484+
461485
# Strip return type annotation
462-
s = re.sub(r"\)\s*->\s.*$", ")", sig)
486+
s = re.sub(r"\)\s*->\s.*$", ")", s)
463487

464488
# Remove parenthesis
465489
s = re.sub(r"^\((.*)\)$", r"\1", s).strip()

tests/test_ext_autosummary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_mangle_signature():
6363
(a=1, b=2, c=3) :: ([a, b, c])
6464
(a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c])
6565
(a=1, b=T(a=1, b=2), c=3) :: ([a, b, c])
66-
(a: int, b: int) -> str :: (a, b)
66+
(a: Tuple[int, str], b: int) -> str :: (a, b)
6767
"""
6868

6969
TEST = [[y.strip() for y in x.split("::")] for x in TEST.split("\n")

0 commit comments

Comments
 (0)