Fix #8992: autodoc: Failed to resolve types.TracebackType type annotation

The builtin module, ``types.TracebackType`` does not have correct module
name.  This allows to refer it automatically.
This commit is contained in:
Takeshi KOMIYA 2021-03-17 23:35:41 +09:00
parent 9f62b9cccb
commit 8c7e779731
3 changed files with 17 additions and 7 deletions

View File

@ -89,6 +89,7 @@ Bugs fixed
* #8917: autodoc: Raises a warning if function has wrong __globals__ value * #8917: autodoc: Raises a warning if function has wrong __globals__ value
* #8415: autodoc: a TypeVar imported from other module is not resolved (in * #8415: autodoc: a TypeVar imported from other module is not resolved (in
Python 3.7 or above) Python 3.7 or above)
* #8992: autodoc: Failed to resolve types.TracebackType type annotation
* #8905: html: html_add_permalinks=None and html_add_permalinks="" are ignored * #8905: html: html_add_permalinks=None and html_add_permalinks="" are ignored
* #8380: html search: Paragraphs in search results are not identified as ``<p>`` * #8380: html search: Paragraphs in search results are not identified as ``<p>``
* #8915: html theme: The translation of sphinx_rtd_theme does not work * #8915: html theme: The translation of sphinx_rtd_theme does not work

View File

@ -11,6 +11,7 @@
import sys import sys
import typing import typing
from struct import Struct from struct import Struct
from types import TracebackType
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, TypeVar, Union from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, TypeVar, Union
from docutils import nodes from docutils import nodes
@ -42,6 +43,13 @@ if False:
from typing import Type # NOQA # for python3.5.1 from typing import Type # NOQA # for python3.5.1
# builtin classes that have incorrect __module__
INVALID_BUILTIN_CLASSES = {
Struct: 'struct.Struct', # Before Python 3.9
TracebackType: 'types.TracebackType',
}
# Text like nodes which are initialized with text and rawsource # Text like nodes which are initialized with text and rawsource
TextlikeNode = Union[nodes.Text, nodes.TextElement] TextlikeNode = Union[nodes.Text, nodes.TextElement]
@ -101,9 +109,8 @@ def restify(cls: Optional["Type"]) -> str:
return ':obj:`None`' return ':obj:`None`'
elif cls is Ellipsis: elif cls is Ellipsis:
return '...' return '...'
elif cls is Struct: elif cls in INVALID_BUILTIN_CLASSES:
# Before Python 3.9, struct.Struct class has incorrect __module__. return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
return ':class:`struct.Struct`'
elif inspect.isNewType(cls): elif inspect.isNewType(cls):
return ':class:`%s`' % cls.__name__ return ':class:`%s`' % cls.__name__
elif types_Union and isinstance(cls, types_Union): elif types_Union and isinstance(cls, types_Union):
@ -276,14 +283,13 @@ def stringify(annotation: Any) -> str:
return repr(annotation) return repr(annotation)
elif annotation is NoneType: elif annotation is NoneType:
return 'None' return 'None'
elif annotation in INVALID_BUILTIN_CLASSES:
return INVALID_BUILTIN_CLASSES[annotation]
elif (getattr(annotation, '__module__', None) == 'builtins' and elif (getattr(annotation, '__module__', None) == 'builtins' and
hasattr(annotation, '__qualname__')): hasattr(annotation, '__qualname__')):
return annotation.__qualname__ return annotation.__qualname__
elif annotation is Ellipsis: elif annotation is Ellipsis:
return '...' return '...'
elif annotation is Struct:
# Before Python 3.9, struct.Struct class has incorrect __module__.
return 'struct.Struct'
if sys.version_info >= (3, 7): # py37+ if sys.version_info >= (3, 7): # py37+
return _stringify_py37(annotation) return _stringify_py37(annotation)

View File

@ -11,6 +11,7 @@
import sys import sys
from numbers import Integral from numbers import Integral
from struct import Struct from struct import Struct
from types import TracebackType
from typing import (Any, Callable, Dict, Generator, List, NewType, Optional, Tuple, TypeVar, from typing import (Any, Callable, Dict, Generator, List, NewType, Optional, Tuple, TypeVar,
Union) Union)
@ -45,6 +46,7 @@ def test_restify():
assert restify(None) == ":obj:`None`" assert restify(None) == ":obj:`None`"
assert restify(Integral) == ":class:`numbers.Integral`" assert restify(Integral) == ":class:`numbers.Integral`"
assert restify(Struct) == ":class:`struct.Struct`" assert restify(Struct) == ":class:`struct.Struct`"
assert restify(TracebackType) == ":class:`types.TracebackType`"
assert restify(Any) == ":obj:`Any`" assert restify(Any) == ":obj:`Any`"
@ -133,7 +135,8 @@ def test_stringify():
assert stringify(str) == "str" assert stringify(str) == "str"
assert stringify(None) == "None" assert stringify(None) == "None"
assert stringify(Integral) == "numbers.Integral" assert stringify(Integral) == "numbers.Integral"
assert restify(Struct) == ":class:`struct.Struct`" assert stringify(Struct) == "struct.Struct"
assert stringify(TracebackType) == "types.TracebackType"
assert stringify(Any) == "Any" assert stringify(Any) == "Any"