Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/sphinx-doc/sphinx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2021-09-14 17:39:47 +0300
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2021-09-14 17:59:47 +0300
commited227d7d3c1e9d386b58bacfd7d379fff380db4a (patch)
tree88518d3194e885d9fd5f494ffbbe6a534b360fbc /sphinx/util
parentba2439a105e5a68a44dd3f839e86332889cce451 (diff)
Fix #9630: autodoc: Failed to build xrefs if primary_domain is not 'py'
Autodoc generates reST code that uses raw `:obj:` and `:class:` xrefs to refer the classes and types. But they're fragile because they assume the primary_domain=='py'. This adds `:py:` prefix to these xrefs to make them robust.
Diffstat (limited to 'sphinx/util')
-rw-r--r--sphinx/util/typing.py68
1 files changed, 34 insertions, 34 deletions
diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py
index 8912de8cd..7380f4783 100644
--- a/sphinx/util/typing.py
+++ b/sphinx/util/typing.py
@@ -110,18 +110,18 @@ def restify(cls: Optional[Type]) -> str:
try:
if cls is None or cls is NoneType:
- return ':obj:`None`'
+ return ':py:obj:`None`'
elif cls is Ellipsis:
return '...'
elif cls in INVALID_BUILTIN_CLASSES:
- return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
+ return ':py:class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
elif inspect.isNewType(cls):
if sys.version_info > (3, 10):
# newtypes have correct module info since Python 3.10+
print(cls, type(cls), dir(cls))
- return ':class:`%s.%s`' % (cls.__module__, cls.__name__)
+ return ':py:class:`%s.%s`' % (cls.__module__, cls.__name__)
else:
- return ':class:`%s`' % cls.__name__
+ return ':py:class:`%s`' % cls.__name__
elif UnionType and isinstance(cls, UnionType):
if len(cls.__args__) > 1 and None in cls.__args__:
args = ' | '.join(restify(a) for a in cls.__args__ if a)
@@ -130,12 +130,12 @@ def restify(cls: Optional[Type]) -> str:
return ' | '.join(restify(a) for a in cls.__args__)
elif cls.__module__ in ('__builtin__', 'builtins'):
if hasattr(cls, '__args__'):
- return ':class:`%s`\\ [%s]' % (
+ return ':py:class:`%s`\\ [%s]' % (
cls.__name__,
', '.join(restify(arg) for arg in cls.__args__),
)
else:
- return ':class:`%s`' % cls.__name__
+ return ':py:class:`%s`' % cls.__name__
else:
if sys.version_info >= (3, 7): # py37+
return _restify_py37(cls)
@@ -155,20 +155,20 @@ def _restify_py37(cls: Optional[Type]) -> str:
if len(cls.__args__) > 1 and cls.__args__[-1] is NoneType:
if len(cls.__args__) > 2:
args = ', '.join(restify(a) for a in cls.__args__[:-1])
- return ':obj:`~typing.Optional`\\ [:obj:`~typing.Union`\\ [%s]]' % args
+ return ':py:obj:`~typing.Optional`\\ [:obj:`~typing.Union`\\ [%s]]' % args
else:
- return ':obj:`~typing.Optional`\\ [%s]' % restify(cls.__args__[0])
+ return ':py:obj:`~typing.Optional`\\ [%s]' % restify(cls.__args__[0])
else:
args = ', '.join(restify(a) for a in cls.__args__)
- return ':obj:`~typing.Union`\\ [%s]' % args
+ return ':py:obj:`~typing.Union`\\ [%s]' % args
elif inspect.isgenericalias(cls):
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)
+ text = ':py:class:`~%s.%s`' % (cls.__module__, cls._name)
else:
- text = ':class:`%s.%s`' % (cls.__module__, cls._name)
+ text = ':py:class:`%s.%s`' % (cls.__module__, cls._name)
else:
text = restify(cls.__origin__)
@@ -188,20 +188,20 @@ def _restify_py37(cls: Optional[Type]) -> str:
return text
elif isinstance(cls, typing._SpecialForm):
- return ':obj:`~%s.%s`' % (cls.__module__, cls._name)
+ return ':py:obj:`~%s.%s`' % (cls.__module__, cls._name)
elif hasattr(cls, '__qualname__'):
if cls.__module__ == 'typing':
- return ':class:`~%s.%s`' % (cls.__module__, cls.__qualname__)
+ return ':py:class:`~%s.%s`' % (cls.__module__, cls.__qualname__)
else:
- return ':class:`%s.%s`' % (cls.__module__, cls.__qualname__)
+ return ':py:class:`%s.%s`' % (cls.__module__, cls.__qualname__)
elif isinstance(cls, ForwardRef):
- return ':class:`%s`' % cls.__forward_arg__
+ return ':py:class:`%s`' % cls.__forward_arg__
else:
# not a class (ex. TypeVar)
if cls.__module__ == 'typing':
- return ':obj:`~%s.%s`' % (cls.__module__, cls.__name__)
+ return ':py:obj:`~%s.%s`' % (cls.__module__, cls.__name__)
else:
- return ':obj:`%s.%s`' % (cls.__module__, cls.__name__)
+ return ':py:obj:`%s.%s`' % (cls.__module__, cls.__name__)
def _restify_py36(cls: Optional[Type]) -> str:
@@ -225,9 +225,9 @@ def _restify_py36(cls: Optional[Type]) -> str:
if (isinstance(cls, typing.TupleMeta) and # type: ignore
not hasattr(cls, '__tuple_params__')):
if module == 'typing':
- reftext = ':class:`~typing.%s`' % qualname
+ reftext = ':py:class:`~typing.%s`' % qualname
else:
- reftext = ':class:`%s`' % qualname
+ reftext = ':py:class:`%s`' % qualname
params = cls.__args__
if params:
@@ -237,9 +237,9 @@ def _restify_py36(cls: Optional[Type]) -> str:
return reftext
elif isinstance(cls, typing.GenericMeta):
if module == 'typing':
- reftext = ':class:`~typing.%s`' % qualname
+ reftext = ':py:class:`~typing.%s`' % qualname
else:
- reftext = ':class:`%s`' % qualname
+ reftext = ':py:class:`%s`' % qualname
if cls.__args__ is None or len(cls.__args__) <= 2:
params = cls.__args__
@@ -262,38 +262,38 @@ def _restify_py36(cls: Optional[Type]) -> str:
if len(params) > 1 and params[-1] is NoneType:
if len(params) > 2:
param_str = ", ".join(restify(p) for p in params[:-1])
- return (':obj:`~typing.Optional`\\ '
- '[:obj:`~typing.Union`\\ [%s]]' % param_str)
+ return (':py:obj:`~typing.Optional`\\ '
+ '[:py:obj:`~typing.Union`\\ [%s]]' % param_str)
else:
- return ':obj:`~typing.Optional`\\ [%s]' % restify(params[0])
+ return ':py:obj:`~typing.Optional`\\ [%s]' % restify(params[0])
else:
param_str = ', '.join(restify(p) for p in params)
- return ':obj:`~typing.Union`\\ [%s]' % param_str
+ return ':py:obj:`~typing.Union`\\ [%s]' % param_str
else:
- return ':obj:`Union`'
+ return ':py:obj:`Union`'
elif hasattr(cls, '__qualname__'):
if cls.__module__ == 'typing':
- return ':class:`~%s.%s`' % (cls.__module__, cls.__qualname__)
+ return ':py:class:`~%s.%s`' % (cls.__module__, cls.__qualname__)
else:
- return ':class:`%s.%s`' % (cls.__module__, cls.__qualname__)
+ return ':py:class:`%s.%s`' % (cls.__module__, cls.__qualname__)
elif hasattr(cls, '_name'):
# SpecialForm
if cls.__module__ == 'typing':
- return ':obj:`~%s.%s`' % (cls.__module__, cls._name)
+ return ':py:obj:`~%s.%s`' % (cls.__module__, cls._name)
else:
- return ':obj:`%s.%s`' % (cls.__module__, cls._name)
+ return ':py:obj:`%s.%s`' % (cls.__module__, cls._name)
elif hasattr(cls, '__name__'):
# not a class (ex. TypeVar)
if cls.__module__ == 'typing':
- return ':obj:`~%s.%s`' % (cls.__module__, cls.__name__)
+ return ':py:obj:`~%s.%s`' % (cls.__module__, cls.__name__)
else:
- return ':obj:`%s.%s`' % (cls.__module__, cls.__name__)
+ return ':py:obj:`%s.%s`' % (cls.__module__, cls.__name__)
else:
# others (ex. Any)
if cls.__module__ == 'typing':
- return ':obj:`~%s.%s`' % (cls.__module__, qualname)
+ return ':py:obj:`~%s.%s`' % (cls.__module__, qualname)
else:
- return ':obj:`%s.%s`' % (cls.__module__, qualname)
+ return ':py:obj:`%s.%s`' % (cls.__module__, qualname)
def stringify(annotation: Any) -> str: