@@ -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
4141if False :
4242 # For type annotation
@@ -117,7 +117,7 @@ def restify(cls: Optional[Type]) -> str:
117117 return ':class:`%s`' % INVALID_BUILTIN_CLASSES [cls ]
118118 elif inspect .isNewType (cls ):
119119 return ':class:`%s`' % cls .__name__
120- elif types_Union and isinstance (cls , types_Union ):
120+ elif UnionType and isinstance (cls , UnionType ):
121121 if len (cls .__args__ ) > 1 and None in cls .__args__ :
122122 args = ' | ' .join (restify (a ) for a in cls .__args__ if a )
123123 return 'Optional[%s]' % args
@@ -343,7 +343,7 @@ def _stringify_py37(annotation: Any) -> str:
343343 elif hasattr (annotation , '__origin__' ):
344344 # instantiated generic provided by a user
345345 qualname = stringify (annotation .__origin__ )
346- 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+)
347347 qualname = 'types.Union'
348348 else :
349349 # we weren't able to extract the base type, appending arguments would
0 commit comments