@@ -332,6 +332,23 @@ def _toposort(graph: Dict[T, Set[T]]) -> Generator[T, None, None]:
332332 assert not graph , "A cyclic dependency exists amongst %r" % graph
333333
334334
335+ def _return_annotation (name , module , obj , link = None ):
336+ try :
337+ annot = typing .get_type_hints (obj ).get ('return' , '' )
338+ except NameError as e :
339+ warn ("Error handling return annotation for {}: {}" .format (name , e .args [0 ]))
340+ annot = inspect .signature (inspect .unwrap (obj )).return_annotation
341+ if annot == inspect .Parameter .empty :
342+ annot = ''
343+ if not annot :
344+ return ''
345+ s = inspect .formatannotation (annot ).replace (' ' , '\N{NBSP} ' ) # Better line breaks
346+ if link :
347+ from pdoc .html_helpers import _linkify
348+ s = re .sub (r'[\w\.]+' , partial (_linkify , link = link , module = module ), s )
349+ return s
350+
351+
335352def link_inheritance (context : Context = None ):
336353 """
337354 Link inheritance relationsships between `pdoc.Class` objects
@@ -1076,20 +1093,7 @@ def _is_async(self):
10761093
10771094 def return_annotation (self , * , link = None ):
10781095 """Formatted function return type annotation or empty string if none."""
1079- try :
1080- annot = typing .get_type_hints (self .obj ).get ('return' , '' )
1081- except NameError as e :
1082- warn ("Error handling return annotation for {}: {}" .format (self .name , e .args [0 ]))
1083- annot = inspect .signature (inspect .unwrap (self .obj )).return_annotation
1084- if annot == inspect .Parameter .empty :
1085- annot = ''
1086- if not annot :
1087- return ''
1088- s = inspect .formatannotation (annot ).replace (' ' , '\N{NBSP} ' ) # Better line breaks
1089- if link :
1090- from pdoc .html_helpers import _linkify
1091- s = re .sub (r'[\w\.]+' , partial (_linkify , link = link , module = self .module ), s )
1092- return s
1096+ return _return_annotation (self .name , self .module , self .obj , link = link )
10931097
10941098 def params (self , * , annotate : bool = False , link : Callable [[Doc ], str ] = None ) -> List [str ]:
10951099 """
@@ -1235,6 +1239,10 @@ def qualname(self):
12351239 def refname (self ):
12361240 return (self .cls .refname if self .cls else self .module .refname ) + '.' + self .name
12371241
1242+ def type_annotation (self , * , link = None ):
1243+ """Formatted variable type annotation or empty string if none."""
1244+ return _return_annotation (self .name , self .module , self .obj , link = link )
1245+
12381246
12391247class External (Doc ):
12401248 """
0 commit comments