From 8932dfd92b1d4869fff087e7111d4e576693a586 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 9 May 2020 14:25:29 +0900 Subject: [PATCH] Suppress arguments if all system defined TypeVars on py39 --- CHANGES | 1 + sphinx/util/typing.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index d426fa48c87..b322c7801f7 100644 --- a/CHANGES +++ b/CHANGES @@ -85,6 +85,7 @@ Bugs fixed * #6857: autodoc: failed to detect a classmethod on Enum class * #7562: autodoc: a typehint contains spaces is wrongly rendered under autodoc_typehints='description' mode +* #7637: autodoc: system defined TypeVars are shown in Python 3.9 * #7535: sphinx-autogen: crashes when custom template uses inheritance * #7536: sphinx-autogen: crashes when template uses i18n feature * #2785: html: Bad alignment of equation links diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py index 4644b137897..08bf7203bdb 100644 --- a/sphinx/util/typing.py +++ b/sphinx/util/typing.py @@ -39,6 +39,12 @@ Inventory = Dict[str, Dict[str, Tuple[str, str, str, str]]] +def is_system_TypeVar(typ: Any) -> bool: + """Check *typ* is system defined TypeVar.""" + modname = getattr(typ, '__module__', '') + return modname == 'typing' and isinstance(typ, TypeVar) # type: ignore + + def stringify(annotation: Any) -> str: """Stringify type annotation object.""" if isinstance(annotation, str): @@ -96,7 +102,8 @@ def _stringify_py37(annotation: Any) -> str: return '%s[[%s], %s]' % (qualname, args, returns) elif str(annotation).startswith('typing.Annotated'): # for py39+ return stringify(annotation.__args__[0]) - elif getattr(annotation, '_special', False): + elif all(is_system_TypeVar(a) for a in annotation.__args__): + # Suppress arguments if all system defined TypeVars (ex. Dict[KT, VT]) return qualname else: args = ', '.join(stringify(a) for a in annotation.__args__)