@@ -1274,8 +1274,9 @@ def maybe_replace_reprs(a):
1274
1274
# Union[T, None] -> Optional[T]
1275
1275
if (getattr (a , '__origin__' , None ) is typing .Union and
1276
1276
len (a .__args__ ) == 2 and
1277
- a .__args__ [1 ] is type (None )): # noqa: E721
1278
- t = inspect .formatannotation (maybe_replace_reprs (a .__args__ [0 ]))
1277
+ type (None ) in a .__args__ ):
1278
+ t = inspect .formatannotation (
1279
+ maybe_replace_reprs (next (filter (None , a .__args__ ))))
1279
1280
return force_repr (f'Optional[{ t } ]' )
1280
1281
# typing.NewType('T', foo) -> T
1281
1282
module = getattr (a , '__module__' , '' )
@@ -1285,10 +1286,8 @@ def maybe_replace_reprs(a):
1285
1286
if module .startswith ('nptyping.' ):
1286
1287
return force_repr (repr (a ))
1287
1288
# Recurse into args
1288
- try :
1289
+ if hasattr ( a , 'copy_with' ) and hasattr ( a , '__args__' ) :
1289
1290
a = a .copy_with (tuple ([maybe_replace_reprs (arg ) for arg in a .__args__ ]))
1290
- except Exception :
1291
- pass # Not a typing._GenericAlias
1292
1291
return a
1293
1292
1294
1293
return str (inspect .formatannotation (maybe_replace_reprs (annot )))
@@ -1393,7 +1392,7 @@ def return_annotation(self, *, link=None) -> str:
1393
1392
s = annot
1394
1393
else :
1395
1394
s = _formatannotation (annot )
1396
- s = re .sub (r'\b(typing\.)?ForwardRef \((?P<quot>[\"\'])(?P<str>.*?)(?P=quot)\)' ,
1395
+ s = re .sub (r'\bForwardRef \((?P<quot>[\"\'])(?P<str>.*?)(?P=quot)\)' ,
1397
1396
r'\g<str>' , s )
1398
1397
s = s .replace (' ' , '\N{NBSP} ' ) # Better line breaks in html signatures
1399
1398
0 commit comments