@@ -332,6 +332,23 @@ def _toposort(graph: Dict[T, Set[T]]) -> Generator[T, None, None]:
332
332
assert not graph , "A cyclic dependency exists amongst %r" % graph
333
333
334
334
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
+
335
352
def link_inheritance (context : Context = None ):
336
353
"""
337
354
Link inheritance relationsships between `pdoc.Class` objects
@@ -1076,20 +1093,7 @@ def _is_async(self):
1076
1093
1077
1094
def return_annotation (self , * , link = None ):
1078
1095
"""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 )
1093
1097
1094
1098
def params (self , * , annotate : bool = False , link : Callable [[Doc ], str ] = None ) -> List [str ]:
1095
1099
"""
@@ -1235,6 +1239,10 @@ def qualname(self):
1235
1239
def refname (self ):
1236
1240
return (self .cls .refname if self .cls else self .module .refname ) + '.' + self .name
1237
1241
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
+
1238
1246
1239
1247
class External (Doc ):
1240
1248
"""
0 commit comments