Skip to content

Commit 585869a

Browse files
committed
Merge branch '4.x' into 9479_autodoc_mocked_target
2 parents a7daa19 + 155ca3e commit 585869a

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Bugs fixed
2222
----------
2323

2424
* #9487: autodoc: typehint for cached_property is not shown
25+
* #9509: autodoc: AttributeError is raised on failed resolving typehints
2526
* #9481: autosummary: some warnings contain non-existing filenames
2627
* #9481: c domain: some warnings contain non-existing filenames
2728
* #9481: cpp domain: some warnings contain non-existing filenames
@@ -47,6 +48,8 @@ Features added
4748
Bugs fixed
4849
----------
4950

51+
* #9512: sphinx-build: crashed with the HEAD of Python 3.10
52+
5053
Testing
5154
--------
5255

sphinx/util/typing.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def _evaluate(self, globalns: Dict, localns: Dict) -> Any:
3333
ref = _ForwardRef(self.arg)
3434
return ref._eval_type(globalns, localns)
3535

36-
if sys.version_info > (3, 10):
37-
from types import Union as types_Union
38-
else:
39-
types_Union = None
36+
try:
37+
from types import UnionType # type: ignore # python 3.10 or above
38+
except ImportError:
39+
UnionType = None
4040

4141
if False:
4242
# For type annotation
@@ -86,6 +86,9 @@ def get_type_hints(obj: Any, globalns: Dict = None, localns: Dict = None) -> Dic
8686
except NameError:
8787
# Failed to evaluate ForwardRef (maybe TYPE_CHECKING)
8888
return safe_getattr(obj, '__annotations__', {})
89+
except AttributeError:
90+
# Failed to evaluate ForwardRef (maybe not runtime checkable)
91+
return safe_getattr(obj, '__annotations__', {})
8992
except TypeError:
9093
# Invalid object is given. But try to get __annotations__ as a fallback for
9194
# the code using type union operator (PEP 604) in python 3.9 or below.
@@ -114,7 +117,7 @@ def restify(cls: Optional[Type]) -> str:
114117
return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
115118
elif inspect.isNewType(cls):
116119
return ':class:`%s`' % cls.__name__
117-
elif types_Union and isinstance(cls, types_Union):
120+
elif UnionType and isinstance(cls, UnionType):
118121
if len(cls.__args__) > 1 and None in cls.__args__:
119122
args = ' | '.join(restify(a) for a in cls.__args__ if a)
120123
return 'Optional[%s]' % args
@@ -340,7 +343,7 @@ def _stringify_py37(annotation: Any) -> str:
340343
elif hasattr(annotation, '__origin__'):
341344
# instantiated generic provided by a user
342345
qualname = stringify(annotation.__origin__)
343-
elif types_Union and isinstance(annotation, types_Union): # types.Union (for py3.10+)
346+
elif UnionType and isinstance(annotation, UnionType): # types.Union (for py3.10+)
344347
qualname = 'types.Union'
345348
else:
346349
# we weren't able to extract the base type, appending arguments would

0 commit comments

Comments
 (0)