Skip to content

Commit

Permalink
Fix sphinx-doc#9404: autodoc: autoclass raises TypeError for dict-lik…
Browse files Browse the repository at this point in the history
…e object

The autoclass directive raises TypeError when dict-like object (not a
class) is given as a subject.
  • Loading branch information
tk0miya committed Jul 6, 2021
1 parent 996c585 commit dab6ee4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -85,6 +85,8 @@ Bugs fixed
attribute not having any comment
* #9364: autodoc: single element tuple on the default argument value is wrongly
rendered
* #9404: autodoc: TypeError is raised on processing dict-like object (not a
class) via autoclass directive
* #9317: html: Pushing left key causes visiting the next page at the first page
* #9381: html: URL for html_favicon and html_log does not work
* #9270: html theme : pyramid theme generates incorrect logo links
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/autodoc/__init__.py
Expand Up @@ -1744,7 +1744,7 @@ def add_content(self, more_content: Optional[StringList], no_docstring: bool = F
if self.doc_as_attr and not self.get_variable_comment():
try:
more_content = StringList([_('alias of %s') % restify(self.object)], source='')
except AttributeError:
except (AttributeError, ValueError):
pass # Invalid class object is passed.

super().add_content(more_content)
Expand Down
43 changes: 23 additions & 20 deletions sphinx/util/typing.py
Expand Up @@ -105,27 +105,30 @@ def restify(cls: Optional[Type]) -> str:
"""Convert python class to a reST reference."""
from sphinx.util import inspect # lazy loading

if cls is None or cls is NoneType:
return ':obj:`None`'
elif cls is Ellipsis:
return '...'
elif cls in INVALID_BUILTIN_CLASSES:
return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
elif inspect.isNewType(cls):
return ':class:`%s`' % cls.__name__
elif types_Union and isinstance(cls, types_Union):
if len(cls.__args__) > 1 and None in cls.__args__:
args = ' | '.join(restify(a) for a in cls.__args__ if a)
return 'Optional[%s]' % args
else:
return ' | '.join(restify(a) for a in cls.__args__)
elif cls.__module__ in ('__builtin__', 'builtins'):
return ':class:`%s`' % cls.__name__
else:
if sys.version_info >= (3, 7): # py37+
return _restify_py37(cls)
try:
if cls is None or cls is NoneType:
return ':obj:`None`'
elif cls is Ellipsis:
return '...'
elif cls in INVALID_BUILTIN_CLASSES:
return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
elif inspect.isNewType(cls):
return ':class:`%s`' % cls.__name__
elif types_Union and isinstance(cls, types_Union):
if len(cls.__args__) > 1 and None in cls.__args__:
args = ' | '.join(restify(a) for a in cls.__args__ if a)
return 'Optional[%s]' % args
else:
return ' | '.join(restify(a) for a in cls.__args__)
elif cls.__module__ in ('__builtin__', 'builtins'):
return ':class:`%s`' % cls.__name__
else:
return _restify_py36(cls)
if sys.version_info >= (3, 7): # py37+
return _restify_py37(cls)
else:
return _restify_py36(cls)
except TypeError as exc:
raise ValueError from exc


def _restify_py37(cls: Optional[Type]) -> str:
Expand Down

0 comments on commit dab6ee4

Please sign in to comment.