Skip to content

Commit 1fd5f74

Browse files
committed
Fix #9512: sphinx-build: crashed with the HEAD of Python 3.10
Recently, `types.Union` was renamed to `types.UnionType` on the HEAD of 3.10 (refs: python/cpython#27342). After this change, sphinx-build has been crashed because of ImportError.
1 parent f210e45 commit 1fd5f74

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Features added
1616
Bugs fixed
1717
----------
1818

19+
* #9512: sphinx-build: crashed with the HEAD of Python 3.10
20+
1921
Testing
2022
--------
2123

sphinx/util/typing.py

Lines changed: 6 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
@@ -114,7 +114,7 @@ def restify(cls: Optional[Type]) -> str:
114114
return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
115115
elif inspect.isNewType(cls):
116116
return ':class:`%s`' % cls.__name__
117-
elif types_Union and isinstance(cls, types_Union):
117+
elif UnionType and isinstance(cls, UnionType):
118118
if len(cls.__args__) > 1 and None in cls.__args__:
119119
args = ' | '.join(restify(a) for a in cls.__args__ if a)
120120
return 'Optional[%s]' % args
@@ -337,7 +337,7 @@ def _stringify_py37(annotation: Any) -> str:
337337
elif hasattr(annotation, '__origin__'):
338338
# instantiated generic provided by a user
339339
qualname = stringify(annotation.__origin__)
340-
elif types_Union and isinstance(annotation, types_Union): # types.Union (for py3.10+)
340+
elif UnionType and isinstance(annotation, UnionType): # types.Union (for py3.10+)
341341
qualname = 'types.Union'
342342
else:
343343
# we weren't able to extract the base type, appending arguments would

0 commit comments

Comments
 (0)