Backport bugfixes from upstream 4.1.x branch for compatibility with Python 3.10-rc2

This commit is contained in:
Karolina Surma 2021-09-16 10:50:00 +02:00
parent cc7f8dfbf3
commit f0f09a5087
4 changed files with 113 additions and 1 deletions

View File

@ -0,0 +1,35 @@
From 648af37d864b5d76bbe833785d664e40d56d9768 Mon Sep 17 00:00:00 2001
From: Takeshi KOMIYA <i.tkomiya@gmail.com>
Date: Wed, 11 Aug 2021 01:36:46 +0900
Subject: [PATCH] Fix #9537: autodoc: Some typing.* objects are broken
At the HEAD of 3.10, the implementation of `typing._GenericAlias` has
been changed to have correct _name and __name__.
---
sphinx/util/typing.py | 6 ++++--
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py
index f856950bf2..df5277ae7b 100644
--- a/sphinx/util/typing.py
+++ b/sphinx/util/typing.py
@@ -148,7 +148,9 @@ def _restify_py37(cls: Optional[Type]) -> str:
args = ', '.join(restify(a) for a in cls.__args__)
return ':obj:`~typing.Union`\\ [%s]' % args
elif inspect.isgenericalias(cls):
- if getattr(cls, '_name', None):
+ if isinstance(cls.__origin__, typing._SpecialForm):
+ text = restify(cls.__origin__) # type: ignore
+ elif getattr(cls, '_name', None):
if cls.__module__ == 'typing':
text = ':class:`~%s.%s`' % (cls.__module__, cls._name)
else:
@@ -344,7 +346,7 @@ def _stringify_py37(annotation: Any) -> str:
if not isinstance(annotation.__args__, (list, tuple)):
# broken __args__ found
pass
- elif qualname == 'Union':
+ elif qualname in ('Optional', 'Union'):
if len(annotation.__args__) > 1 and annotation.__args__[-1] is NoneType:
if len(annotation.__args__) > 2:
args = ', '.join(stringify(a) for a in annotation.__args__[:-1])

View File

@ -0,0 +1,36 @@
From d0c97e9eb57126e38b7b018a101df1ecb1ad12b8 Mon Sep 17 00:00:00 2001
From: Takeshi KOMIYA <i.tkomiya@gmail.com>
Date: Sat, 31 Jul 2021 01:41:35 +0900
Subject: [PATCH] Fix #9504: autodoc: generate incorrect reference to the
parent class
Autodoc generates incorrect references to the parent class the target
class inherites the class having `_name` attribute. It conciders the
parent is a kind of SpecialForm'ed class by mistake. This uses
`isinstance(X, SpecialForm)` to check that.
Note: SpecialForm became a class since Python 3.7.
---
CHANGES | 2 ++
sphinx/util/typing.py | 8 ++------
2 files changed, 4 insertions(+), 6 deletions(-)
diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py
index 012d32e524..f856950bf2 100644
--- a/sphinx/util/typing.py
+++ b/sphinx/util/typing.py
@@ -171,12 +171,8 @@ def _restify_py37(cls: Optional[Type]) -> str:
text += r"\ [%s]" % ", ".join(restify(a) for a in cls.__args__)
return text
- 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 isinstance(cls, typing._SpecialForm):
+ return ':obj:`~%s.%s`' % (cls.__module__, cls._name)
elif hasattr(cls, '__qualname__'):
if cls.__module__ == 'typing':
return ':class:`~%s.%s`' % (cls.__module__, cls.__qualname__)

View File

@ -28,7 +28,7 @@ Name: python-sphinx
#global prerel ...
%global upstream_version %{general_version}%{?prerel}
Version: %{general_version}%{?prerel:~%{prerel}}
Release: 2%{?dist}
Release: 3%{?dist}
Epoch: 1
Summary: Python documentation generator
@ -46,10 +46,21 @@ Source0: %{pypi_source %{upstream_name} %{upstream_version}}
Patch1: sphinx-test_theming.diff
# `types.Union` was renamed to `types.UnionType` on the HEAD of Python 3.10
# (refs: python/cpython#27342). Afterwars, sphinx-build crashes because of ImportError
# Merged upstream: https://github.com/sphinx-doc/sphinx/pull/9513
Patch2: rename-types-Union-to-types-UnionType.patch
# Fix test failures with python-pygments 2.10+
# https://github.com/sphinx-doc/sphinx/pull/9557
Patch3: fix-tests-with-pygments-210.patch
# Some objects under ``typing`` module are not displayed well
# with the HEAD of Python 3.10.0rc2+
# Merged upstream: https://github.com/sphinx-doc/sphinx/pull/9538
Patch4: display-typing-objects-correctly-with-Python-310.patch
# Render typing.Annotated correctly with Python 3.10
# Merged upstream: https://github.com/sphinx-doc/sphinx/pull/9590
Patch5: render-typing-Annotated-correctly-with-Python-3.10.patch
# Generate correct reference to the parent class
# Merged upstream: https://github.com/sphinx-doc/sphinx/pull/9515/
Patch6: generate-correct-reference-to-parent-class.patch
BuildArch: noarch
@ -371,6 +382,10 @@ mkdir %{buildroot}%{python3_sitelib}/sphinxcontrib
%changelog
* Thu Sep 16 2021 Karolina Surma <ksurma@redhat.com> - 1:4.1.2-3
- Display typing objects correctly with Python 3.10 (fix FTBFS)
- Generate correct reference to parent class if class has `_name` attribute
* Wed Aug 18 2021 Karolina Surma <ksurma@redhat.com> - 1:4.1.2-2
- Patch python-sphinx to work with python-pygments >=2.10

View File

@ -0,0 +1,26 @@
From b82d3ef05a75b6dad9679f8746db5ef89785c7c0 Mon Sep 17 00:00:00 2001
From: Takeshi KOMIYA <i.tkomiya@gmail.com>
Date: Sun, 29 Aug 2021 15:40:49 +0900
Subject: [PATCH] Fix #9589: autodoc: typing.Annotated has wrongly been
rendered
At the HEAD of 3.10, the implementation of `typing.Annotated` has
been changed to have __qualname__.
---
CHANGES | 4 ++--
sphinx/util/typing.py | 2 ++
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py
index df5277ae7b..35f808211b 100644
--- a/sphinx/util/typing.py
+++ b/sphinx/util/typing.py
@@ -306,6 +306,8 @@ def stringify(annotation: Any) -> str:
return 'None'
elif annotation in INVALID_BUILTIN_CLASSES:
return INVALID_BUILTIN_CLASSES[annotation]
+ elif str(annotation).startswith('typing.Annotated'): # for py310+
+ pass
elif (getattr(annotation, '__module__', None) == 'builtins' and
getattr(annotation, '__qualname__', None)):
return annotation.__qualname__