@@ -1167,6 +1167,20 @@ def _link_inheritance(self):
1167
1167
del self ._super_members
1168
1168
1169
1169
1170
+ def _formatannotation (annot ):
1171
+ """
1172
+ Format annotation, properly handling NewType types
1173
+
1174
+ >>> _formatannotation(NewType('MyType', str))
1175
+ 'MyType'
1176
+ """
1177
+ is_new_type = (getattr (annot , '__qualname__' , '' ).startswith ('NewType.' ) and
1178
+ getattr (annot , '__module__' , '' ) == 'typing' )
1179
+ if is_new_type :
1180
+ return annot .__name__
1181
+ return inspect .formatannotation (annot )
1182
+
1183
+
1170
1184
class Function (Doc ):
1171
1185
"""
1172
1186
Representation of documentation for a function or method.
@@ -1265,7 +1279,7 @@ def return_annotation(self, *, link=None) -> str:
1265
1279
if isinstance (annot , str ):
1266
1280
s = annot
1267
1281
else :
1268
- s = inspect . formatannotation (annot )
1282
+ s = _formatannotation (annot )
1269
1283
s = re .sub (r'\b(typing\.)?ForwardRef\((?P<quot>[\"\'])(?P<str>.*?)(?P=quot)\)' ,
1270
1284
r'\g<str>' , s )
1271
1285
s = s .replace (' ' , '\N{NBSP} ' ) # Better line breaks in html signatures
@@ -1371,7 +1385,7 @@ def __repr__(self):
1371
1385
1372
1386
formatted = p .name
1373
1387
if p .annotation is not EMPTY :
1374
- annotation = inspect . formatannotation (p .annotation ).replace (' ' , '\N{NBSP} ' )
1388
+ annotation = _formatannotation (p .annotation ).replace (' ' , '\N{NBSP} ' )
1375
1389
# "Eval" forward-declarations (typing string literals)
1376
1390
if isinstance (p .annotation , str ):
1377
1391
annotation = annotation .strip ("'" )
0 commit comments