@@ -299,18 +299,25 @@ def _restify_py36(cls: Optional[Type]) -> str:
299299 return ':py:obj:`%s.%s`' % (cls .__module__ , qualname )
300300
301301
302- def stringify (annotation : Any , smartref : bool = False ) -> str :
302+ def stringify (annotation : Any , mode : str = 'fully-qualified-except-typing' ) -> str :
303303 """Stringify type annotation object.
304304
305- :param smartref: If true, add "~" prefix to the result to remove the leading
306- module and class names from the reference text
305+ :param mode: Specify a method how annotations will be stringified.
306+
307+ 'fully-qualified-except-typing'
308+ Show the module name and qualified name of the annotation except
309+ the "typing" module.
310+ 'smart'
311+ Show the name of the annotation.
312+ 'fully-qualified'
313+ Show the module name and qualified name of the annotation.
307314 """
308315 from sphinx .util import inspect # lazy loading
309316
310- if smartref :
311- prefix = '~'
317+ if mode == 'smart' :
318+ modprefix = '~'
312319 else :
313- prefix = ''
320+ modprefix = ''
314321
315322 if isinstance (annotation , str ):
316323 if annotation .startswith ("'" ) and annotation .endswith ("'" ):
@@ -319,22 +326,23 @@ def stringify(annotation: Any, smartref: bool = False) -> str:
319326 else :
320327 return annotation
321328 elif isinstance (annotation , TypeVar ):
322- if annotation .__module__ == 'typing' :
329+ if (annotation .__module__ == 'typing' and
330+ mode in ('fully-qualified-except-typing' , 'smart' )):
323331 return annotation .__name__
324332 else :
325- return prefix + '.' .join ([annotation .__module__ , annotation .__name__ ])
333+ return modprefix + '.' .join ([annotation .__module__ , annotation .__name__ ])
326334 elif inspect .isNewType (annotation ):
327335 if sys .version_info > (3 , 10 ):
328336 # newtypes have correct module info since Python 3.10+
329- return prefix + '%s.%s' % (annotation .__module__ , annotation .__name__ )
337+ return modprefix + '%s.%s' % (annotation .__module__ , annotation .__name__ )
330338 else :
331339 return annotation .__name__
332340 elif not annotation :
333341 return repr (annotation )
334342 elif annotation is NoneType :
335343 return 'None'
336344 elif annotation in INVALID_BUILTIN_CLASSES :
337- return prefix + INVALID_BUILTIN_CLASSES [annotation ]
345+ return modprefix + INVALID_BUILTIN_CLASSES [annotation ]
338346 elif str (annotation ).startswith ('typing.Annotated' ): # for py310+
339347 pass
340348 elif (getattr (annotation , '__module__' , None ) == 'builtins' and
@@ -347,12 +355,12 @@ def stringify(annotation: Any, smartref: bool = False) -> str:
347355 return '...'
348356
349357 if sys .version_info >= (3 , 7 ): # py37+
350- return _stringify_py37 (annotation , smartref )
358+ return _stringify_py37 (annotation , mode )
351359 else :
352- return _stringify_py36 (annotation , smartref )
360+ return _stringify_py36 (annotation , mode )
353361
354362
355- def _stringify_py37 (annotation : Any , smartref : bool = False ) -> str :
363+ def _stringify_py37 (annotation : Any , mode : str = 'fully-qualified-except-typing' ) -> str :
356364 """stringify() for py37+."""
357365 module = getattr (annotation , '__module__' , None )
358366 modprefix = ''
@@ -364,19 +372,21 @@ def _stringify_py37(annotation: Any, smartref: bool = False) -> str:
364372 elif getattr (annotation , '__qualname__' , None ):
365373 qualname = annotation .__qualname__
366374 else :
367- qualname = stringify (annotation .__origin__ ) # ex. Union
375+ qualname = stringify (annotation .__origin__ ). replace ( 'typing.' , '' ) # ex. Union
368376
369- if smartref :
377+ if mode == 'smart' :
370378 modprefix = '~%s.' % module
379+ elif mode == 'fully-qualified' :
380+ modprefix = '%s.' % module
371381 elif hasattr (annotation , '__qualname__' ):
372- if smartref :
382+ if mode == 'smart' :
373383 modprefix = '~%s.' % module
374384 else :
375385 modprefix = '%s.' % module
376386 qualname = annotation .__qualname__
377387 elif hasattr (annotation , '__origin__' ):
378388 # instantiated generic provided by a user
379- qualname = stringify (annotation .__origin__ , smartref )
389+ qualname = stringify (annotation .__origin__ , mode )
380390 elif UnionType and isinstance (annotation , UnionType ): # types.Union (for py3.10+)
381391 qualname = 'types.Union'
382392 else :
@@ -391,13 +401,13 @@ def _stringify_py37(annotation: Any, smartref: bool = False) -> str:
391401 elif qualname in ('Optional' , 'Union' ):
392402 if len (annotation .__args__ ) > 1 and annotation .__args__ [- 1 ] is NoneType :
393403 if len (annotation .__args__ ) > 2 :
394- args = ', ' .join (stringify (a , smartref ) for a in annotation .__args__ [:- 1 ])
404+ args = ', ' .join (stringify (a , mode ) for a in annotation .__args__ [:- 1 ])
395405 return '%sOptional[%sUnion[%s]]' % (modprefix , modprefix , args )
396406 else :
397407 return '%sOptional[%s]' % (modprefix ,
398- stringify (annotation .__args__ [0 ], smartref ))
408+ stringify (annotation .__args__ [0 ], mode ))
399409 else :
400- args = ', ' .join (stringify (a , smartref ) for a in annotation .__args__ )
410+ args = ', ' .join (stringify (a , mode ) for a in annotation .__args__ )
401411 return '%sUnion[%s]' % (modprefix , args )
402412 elif qualname == 'types.Union' :
403413 if len (annotation .__args__ ) > 1 and None in annotation .__args__ :
@@ -406,25 +416,25 @@ def _stringify_py37(annotation: Any, smartref: bool = False) -> str:
406416 else :
407417 return ' | ' .join (stringify (a ) for a in annotation .__args__ )
408418 elif qualname == 'Callable' :
409- args = ', ' .join (stringify (a , smartref ) for a in annotation .__args__ [:- 1 ])
410- returns = stringify (annotation .__args__ [- 1 ], smartref )
419+ args = ', ' .join (stringify (a , mode ) for a in annotation .__args__ [:- 1 ])
420+ returns = stringify (annotation .__args__ [- 1 ], mode )
411421 return '%s%s[[%s], %s]' % (modprefix , qualname , args , returns )
412422 elif qualname == 'Literal' :
413423 args = ', ' .join (repr (a ) for a in annotation .__args__ )
414424 return '%s%s[%s]' % (modprefix , qualname , args )
415425 elif str (annotation ).startswith ('typing.Annotated' ): # for py39+
416- return stringify (annotation .__args__ [0 ], smartref )
426+ return stringify (annotation .__args__ [0 ], mode )
417427 elif all (is_system_TypeVar (a ) for a in annotation .__args__ ):
418428 # Suppress arguments if all system defined TypeVars (ex. Dict[KT, VT])
419429 return modprefix + qualname
420430 else :
421- args = ', ' .join (stringify (a , smartref ) for a in annotation .__args__ )
431+ args = ', ' .join (stringify (a , mode ) for a in annotation .__args__ )
422432 return '%s%s[%s]' % (modprefix , qualname , args )
423433
424434 return modprefix + qualname
425435
426436
427- def _stringify_py36 (annotation : Any , smartref : bool = False ) -> str :
437+ def _stringify_py36 (annotation : Any , mode : str = 'fully-qualified-except-typing' ) -> str :
428438 """stringify() for py36."""
429439 module = getattr (annotation , '__module__' , None )
430440 modprefix = ''
@@ -440,10 +450,12 @@ def _stringify_py36(annotation: Any, smartref: bool = False) -> str:
440450 else :
441451 qualname = repr (annotation ).replace ('typing.' , '' )
442452
443- if smartref :
453+ if mode == 'smart' :
444454 modprefix = '~%s.' % module
455+ elif mode == 'fully-qualified' :
456+ modprefix = '%s.' % module
445457 elif hasattr (annotation , '__qualname__' ):
446- if smartref :
458+ if mode == 'smart' :
447459 modprefix = '~%s.' % module
448460 else :
449461 modprefix = '%s.' % module
@@ -455,7 +467,7 @@ def _stringify_py36(annotation: Any, smartref: bool = False) -> str:
455467 not hasattr (annotation , '__tuple_params__' )): # for Python 3.6
456468 params = annotation .__args__
457469 if params :
458- param_str = ', ' .join (stringify (p , smartref ) for p in params )
470+ param_str = ', ' .join (stringify (p , mode ) for p in params )
459471 return '%s%s[%s]' % (modprefix , qualname , param_str )
460472 else :
461473 return modprefix + qualname
@@ -466,25 +478,25 @@ def _stringify_py36(annotation: Any, smartref: bool = False) -> str:
466478 elif annotation .__origin__ == Generator : # type: ignore
467479 params = annotation .__args__ # type: ignore
468480 else : # typing.Callable
469- args = ', ' .join (stringify (arg , smartref ) for arg
481+ args = ', ' .join (stringify (arg , mode ) for arg
470482 in annotation .__args__ [:- 1 ]) # type: ignore
471483 result = stringify (annotation .__args__ [- 1 ]) # type: ignore
472484 return '%s%s[[%s], %s]' % (modprefix , qualname , args , result )
473485 if params is not None :
474- param_str = ', ' .join (stringify (p , smartref ) for p in params )
486+ param_str = ', ' .join (stringify (p , mode ) for p in params )
475487 return '%s%s[%s]' % (modprefix , qualname , param_str )
476488 elif (hasattr (annotation , '__origin__' ) and
477489 annotation .__origin__ is typing .Union ):
478490 params = annotation .__args__
479491 if params is not None :
480492 if len (params ) > 1 and params [- 1 ] is NoneType :
481493 if len (params ) > 2 :
482- param_str = ", " .join (stringify (p , smartref ) for p in params [:- 1 ])
494+ param_str = ", " .join (stringify (p , mode ) for p in params [:- 1 ])
483495 return '%sOptional[%sUnion[%s]]' % (modprefix , modprefix , param_str )
484496 else :
485- return '%sOptional[%s]' % (modprefix , stringify (params [0 ]))
497+ return '%sOptional[%s]' % (modprefix , stringify (params [0 ], mode ))
486498 else :
487- param_str = ', ' .join (stringify (p , smartref ) for p in params )
499+ param_str = ', ' .join (stringify (p , mode ) for p in params )
488500 return '%sUnion[%s]' % (modprefix , param_str )
489501
490502 return modprefix + qualname
0 commit comments