diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-12-31 14:10:55 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-12-31 14:25:05 +0900 |
commit | f1a051fdfc72a882012f399f99b65bbe7729cb1f (patch) | |
tree | 3a120c0c7c3a873e81673d35c918b5678d2ddf3c /sphinx/util/typing.py | |
parent | a1d501d876693653fcd47e8ffd6e10ca4c48ffb6 (diff) | |
download | sphinx-git-f1a051fdfc72a882012f399f99b65bbe7729cb1f.tar.gz |
Fix #8315: autodoc: Failed to resolve struct.Struct type annotation
The builtin module, ``struct.Struct`` does not have correct module
name since Python 3.8. This allows to refer it automatically.
Diffstat (limited to 'sphinx/util/typing.py')
-rw-r--r-- | sphinx/util/typing.py | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py index 2d4f67bba..f9807ae09 100644 --- a/sphinx/util/typing.py +++ b/sphinx/util/typing.py @@ -10,6 +10,7 @@ import sys import typing +from struct import Struct from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, TypeVar, Union from docutils import nodes @@ -94,6 +95,9 @@ def restify(cls: Optional["Type"]) -> str: return ':obj:`None`' elif cls is Ellipsis: return '...' + elif cls is Struct: + # Before Python 3.9, struct.Struct class has incorrect __module__. + return ':class:`struct.Struct`' elif inspect.isNewType(cls): return ':class:`%s`' % cls.__name__ elif cls.__module__ in ('__builtin__', 'builtins'): @@ -305,6 +309,9 @@ def stringify(annotation: Any) -> str: return annotation.__qualname__ elif annotation is Ellipsis: 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+ return _stringify_py37(annotation) |