Skip to content

Commit

Permalink
Merge pull request #9633 from tk0miya/9630_autosummary_primary_domain
Browse files Browse the repository at this point in the history
Fix #9630: autosummary: Failed to build summary table if primary_domain is not 'py'
  • Loading branch information
tk0miya committed Sep 17, 2021
2 parents ba2439a + bc01207 commit bebd6bd
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 119 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Expand Up @@ -16,6 +16,11 @@ Features added
Bugs fixed
----------

* #9630: autodoc: Failed to build cross references if :confval:`primary_domain`
is not 'py'
* #9630: autosummary: Failed to build summary table if :confval:`primary_domain`
is not 'py'

Testing
--------

Expand Down
4 changes: 2 additions & 2 deletions sphinx/ext/autosummary/__init__.py
Expand Up @@ -444,9 +444,9 @@ def append_row(*column_texts: str) -> None:
for name, sig, summary, real_name in items:
qualifier = 'obj'
if 'nosignatures' not in self.options:
col1 = ':%s:`%s <%s>`\\ %s' % (qualifier, name, real_name, rst.escape(sig))
col1 = ':py:%s:`%s <%s>`\\ %s' % (qualifier, name, real_name, rst.escape(sig))
else:
col1 = ':%s:`%s <%s>`' % (qualifier, name, real_name)
col1 = ':py:%s:`%s <%s>`' % (qualifier, name, real_name)
col2 = summary
append_row(col1, col2)

Expand Down
68 changes: 34 additions & 34 deletions sphinx/util/typing.py
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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__)

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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__
Expand All @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions tests/test_ext_autodoc.py
Expand Up @@ -984,7 +984,7 @@ def test_autodoc_inner_class(app):
' .. py:attribute:: Outer.factory',
' :module: target',
'',
' alias of :class:`dict`'
' alias of :py:class:`dict`'
]

actual = do_autodoc(app, 'class', 'target.Outer.Inner', options)
Expand All @@ -1009,7 +1009,7 @@ def test_autodoc_inner_class(app):
'',
'.. py:class:: InnerChild()',
' :module: target', '',
' Bases: :class:`target.Outer.Inner`',
' Bases: :py:class:`target.Outer.Inner`',
'',
' InnerChild docstring',
'',
Expand Down Expand Up @@ -1750,7 +1750,7 @@ def test_autodoc_typed_instance_variables(app):
'.. py:attribute:: Alias',
' :module: target.typed_vars',
'',
' alias of :class:`target.typed_vars.Derived`',
' alias of :py:class:`target.typed_vars.Derived`',
'',
'.. py:class:: Class()',
' :module: target.typed_vars',
Expand Down Expand Up @@ -1915,12 +1915,12 @@ def test_autodoc_GenericAlias(app):
' .. py:attribute:: Class.T',
' :module: target.genericalias',
'',
' alias of :class:`~typing.List`\\ [:class:`int`]',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
'.. py:attribute:: T',
' :module: target.genericalias',
'',
' alias of :class:`~typing.List`\\ [:class:`int`]',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
]
else:
assert list(actual) == [
Expand All @@ -1937,15 +1937,15 @@ def test_autodoc_GenericAlias(app):
'',
' A list of int',
'',
' alias of :class:`~typing.List`\\ [:class:`int`]',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
'',
'.. py:data:: T',
' :module: target.genericalias',
'',
' A list of int',
'',
' alias of :class:`~typing.List`\\ [:class:`int`]',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
]

Expand Down Expand Up @@ -1977,7 +1977,7 @@ def test_autodoc_TypeVar(app):
'',
' T6',
'',
' alias of :class:`int`',
' alias of :py:class:`int`',
'',
'',
'.. py:data:: T1',
Expand Down Expand Up @@ -2017,15 +2017,15 @@ def test_autodoc_TypeVar(app):
'',
' T6',
'',
' alias of :class:`int`',
' alias of :py:class:`int`',
'',
'',
'.. py:data:: T7',
' :module: target.typevar',
'',
' T7',
'',
" alias of TypeVar('T7', bound=\\ :class:`int`)",
" alias of TypeVar('T7', bound=\\ :py:class:`int`)",
'',
]

Expand Down
4 changes: 2 additions & 2 deletions tests/test_ext_autodoc_autoattribute.py
Expand Up @@ -167,7 +167,7 @@ def test_autoattribute_GenericAlias(app):
'',
' A list of int',
'',
' alias of :class:`~typing.List`\\ [:class:`int`]',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
]

Expand All @@ -182,7 +182,7 @@ def test_autoattribute_NewType(app):
'',
' T6',
'',
' alias of :class:`int`',
' alias of :py:class:`int`',
'',
]

Expand Down
10 changes: 5 additions & 5 deletions tests/test_ext_autodoc_autoclass.py
Expand Up @@ -265,8 +265,8 @@ def test_show_inheritance_for_subclass_of_generic_type(app):
'.. py:class:: Quux(iterable=(), /)',
' :module: target.classes',
'',
' Bases: :class:`~typing.List`\\ '
'[:obj:`~typing.Union`\\ [:class:`int`, :class:`float`]]',
' Bases: :py:class:`~typing.List`\\ '
'[:py:obj:`~typing.Union`\\ [:py:class:`int`, :py:class:`float`]]',
'',
' A subclass of List[Union[int, float]]',
'',
Expand Down Expand Up @@ -296,7 +296,7 @@ def autodoc_process_bases(app, name, obj, options, bases):
'.. py:class:: Quux(*args, **kwds)',
' :module: target.classes',
'',
' Bases: :class:`int`, :class:`str`',
' Bases: :py:class:`int`, :py:class:`str`',
'',
' A subclass of List[Union[int, float]]',
'',
Expand All @@ -307,7 +307,7 @@ def autodoc_process_bases(app, name, obj, options, bases):
'.. py:class:: Quux(iterable=(), /)',
' :module: target.classes',
'',
' Bases: :class:`int`, :class:`str`',
' Bases: :py:class:`int`, :py:class:`str`',
'',
' A subclass of List[Union[int, float]]',
'',
Expand Down Expand Up @@ -375,7 +375,7 @@ def autodoc_process_docstring(*args):
'.. py:attribute:: Alias',
' :module: target.classes',
'',
' alias of :class:`target.classes.Foo`',
' alias of :py:class:`target.classes.Foo`',
]


Expand Down
4 changes: 2 additions & 2 deletions tests/test_ext_autodoc_autodata.py
Expand Up @@ -96,7 +96,7 @@ def test_autodata_GenericAlias(app):
'',
' A list of int',
'',
' alias of :class:`~typing.List`\\ [:class:`int`]',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
]

Expand All @@ -111,7 +111,7 @@ def test_autodata_NewType(app):
'',
' T6',
'',
' alias of :class:`int`',
' alias of :py:class:`int`',
'',
]

Expand Down

0 comments on commit bebd6bd

Please sign in to comment.