From 771507e073d46b12bcbdc56346a4ef06d2f653f5 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 23 Jul 2021 00:06:33 +0900 Subject: [PATCH 1/2] Fix #9489: autodoc: Custom types using typing.NewType are broken At the HEAD of 3.10, the implementation of ``typing.NewType`` has been changed to the class based. To follow the change, this uses ``isinstance`` on ``sphinx.util.inspect:isNewType()`. --- CHANGES | 2 ++ sphinx/util/inspect.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 8fa1a09d0ec..a4a4030711f 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,8 @@ Features added Bugs fixed ---------- +* #9489: autodoc: Custom types using ``typing.NewType`` are not displayed well + with the HEAD of 3.10 * #9435: linkcheck: Failed to check anchors in github.com Testing diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index 23dd9e9307f..fedc2aa6a8f 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -211,12 +211,15 @@ def getslots(obj: Any) -> Optional[Dict]: def isNewType(obj: Any) -> bool: """Check the if object is a kind of NewType.""" - __module__ = safe_getattr(obj, '__module__', None) - __qualname__ = safe_getattr(obj, '__qualname__', None) - if __module__ == 'typing' and __qualname__ == 'NewType..new_type': - return True + if sys.version_info >= (3, 10): + return isinstance(obj, typing.NewType) else: - return False + __module__ = safe_getattr(obj, '__module__', None) + __qualname__ = safe_getattr(obj, '__qualname__', None) + if __module__ == 'typing' and __qualname__ == 'NewType..new_type': + return True + else: + return False def isenumclass(x: Any) -> bool: From 68fb54806f2ce476159e5607fc303342c29e31be Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 23 Jul 2021 01:35:20 +0900 Subject: [PATCH 2/2] Fix #9490: autodoc: Some typing.* objects are broken At the HEAD of 3.10, the implementation of `typing._SpecialForm` and `typing._BaseGenericAlias` has been changed to support __qualname__. --- CHANGES | 2 ++ sphinx/util/typing.py | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index a4a4030711f..b4ebcf49826 100644 --- a/CHANGES +++ b/CHANGES @@ -21,6 +21,8 @@ Bugs fixed * #9489: autodoc: Custom types using ``typing.NewType`` are not displayed well with the HEAD of 3.10 +* #9490: autodoc: Some objects under ``typing`` module are not displayed well + with the HEAD of 3.10 * #9435: linkcheck: Failed to check anchors in github.com Testing diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py index 4e1b184e0af..f1723c035a2 100644 --- a/sphinx/util/typing.py +++ b/sphinx/util/typing.py @@ -171,17 +171,17 @@ def _restify_py37(cls: Optional[Type]) -> str: text += r"\ [%s]" % ", ".join(restify(a) for a in cls.__args__) return text - elif hasattr(cls, '__qualname__'): - if cls.__module__ == 'typing': - return ':class:`~%s.%s`' % (cls.__module__, cls.__qualname__) - else: - return ':class:`%s.%s`' % (cls.__module__, cls.__qualname__) elif hasattr(cls, '_name'): # SpecialForm if cls.__module__ == 'typing': return ':obj:`~%s.%s`' % (cls.__module__, cls._name) else: return ':obj:`%s.%s`' % (cls.__module__, cls._name) + elif hasattr(cls, '__qualname__'): + if cls.__module__ == 'typing': + return ':class:`~%s.%s`' % (cls.__module__, cls.__qualname__) + else: + return ':class:`%s.%s`' % (cls.__module__, cls.__qualname__) elif isinstance(cls, ForwardRef): return ':class:`%s`' % cls.__forward_arg__ else: @@ -309,7 +309,7 @@ def stringify(annotation: Any) -> str: elif annotation in INVALID_BUILTIN_CLASSES: return INVALID_BUILTIN_CLASSES[annotation] elif (getattr(annotation, '__module__', None) == 'builtins' and - hasattr(annotation, '__qualname__')): + getattr(annotation, '__qualname__', None)): return annotation.__qualname__ elif annotation is Ellipsis: return '...'