Merge pull request #9513 from tk0miya/9512_types.UnionTypes

Fix #9512: sphinx-build: crashed with the HEAD of Python 3.10
This commit is contained in:
Takeshi KOMIYA 2021-07-30 02:18:34 +09:00 committed by GitHub
commit 36e06d67c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View File

@ -46,6 +46,8 @@ Features added
Bugs fixed
----------
* #9512: sphinx-build: crashed with the HEAD of Python 3.10
Testing
--------

View File

@ -33,10 +33,10 @@ else:
ref = _ForwardRef(self.arg)
return ref._eval_type(globalns, localns)
if sys.version_info > (3, 10):
from types import Union as types_Union
else:
types_Union = None
try:
from types import UnionType # type: ignore # python 3.10 or above
except ImportError:
UnionType = None
if False:
# For type annotation
@ -114,7 +114,7 @@ def restify(cls: Optional[Type]) -> str:
return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
elif inspect.isNewType(cls):
return ':class:`%s`' % cls.__name__
elif types_Union and isinstance(cls, types_Union):
elif UnionType and isinstance(cls, UnionType):
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
@ -340,7 +340,7 @@ def _stringify_py37(annotation: Any) -> str:
elif hasattr(annotation, '__origin__'):
# instantiated generic provided by a user
qualname = stringify(annotation.__origin__)
elif types_Union and isinstance(annotation, types_Union): # types.Union (for py3.10+)
elif UnionType and isinstance(annotation, UnionType): # types.Union (for py3.10+)
qualname = 'types.Union'
else:
# we weren't able to extract the base type, appending arguments would