Skip to content

Commit

Permalink
Fix sphinx-doc#9600: autosummary: Typehints including commas confuses…
Browse files Browse the repository at this point in the history
… autosummary
  • Loading branch information
tk0miya committed Sep 4, 2021
1 parent 6c38f68 commit ed3cc81
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -33,6 +33,8 @@ Bugs fixed
are not displayed well
* #9481: autosummary: some warnings contain non-existing filenames
* #9568: autosummary: summarise overlined sectioned headings correctly
* #9600: autosummary: Type annotations which contain commas in autosummary table
are not removed completely
* #9481: c domain: some warnings contain non-existing filenames
* #9481: cpp domain: some warnings contain non-existing filenames
* #9456: html search: abbreation marks are inserted to the search result if
Expand Down
26 changes: 25 additions & 1 deletion sphinx/ext/autosummary/__init__.py
Expand Up @@ -58,6 +58,7 @@
import re
import sys
import warnings
from inspect import Parameter
from os import path
from types import ModuleType
from typing import Any, Dict, List, Optional, Tuple, Type, cast
Expand Down Expand Up @@ -87,6 +88,7 @@
from sphinx.util import logging, rst
from sphinx.util.docutils import (NullReporter, SphinxDirective, SphinxRole, new_document,
switch_source_input)
from sphinx.util.inspect import signature_from_str
from sphinx.util.matching import Matcher
from sphinx.util.typing import OptionSpec
from sphinx.writers.html import HTMLTranslator
Expand Down Expand Up @@ -456,10 +458,32 @@ def strip_arg_typehint(s: str) -> str:
return s.split(':')[0].strip()


def _cleanup_signature(s: str) -> str:
"""Clean up signature using inspect.signautre() for mangle_signature()"""
try:
sig = signature_from_str(s)
parameters = list(sig.parameters.values())
for i, param in enumerate(parameters):
if param.annotation is not Parameter.empty:
# Remove typehints
param = param.replace(annotation=Parameter.empty)
if param.default is not Parameter.empty:
# Replace default value by "None"
param = param.replace(default=None)
parameters[i] = param
sig = sig.replace(parameters=parameters, return_annotation=Parameter.empty)
return str(sig)
except Exception:
# Return the original signature string if failed to clean (ex. parsing error)
return s


def mangle_signature(sig: str, max_chars: int = 30) -> str:
"""Reformat a function signature to a more compact form."""
s = _cleanup_signature(sig)

# Strip return type annotation
s = re.sub(r"\)\s*->\s.*$", ")", sig)
s = re.sub(r"\)\s*->\s.*$", ")", s)

# Remove parenthesis
s = re.sub(r"^\((.*)\)$", r"\1", s).strip()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ext_autosummary.py
Expand Up @@ -63,7 +63,7 @@ def test_mangle_signature():
(a=1, b=2, c=3) :: ([a, b, c])
(a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c])
(a=1, b=T(a=1, b=2), c=3) :: ([a, b, c])
(a: int, b: int) -> str :: (a, b)
(a: Tuple[int, str], b: int) -> str :: (a, b)
"""

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

0 comments on commit ed3cc81

Please sign in to comment.