Skip to content

Commit c2c1320

Browse files
kernchhoppe
andcommitted
REF: Also replace typing.Union[None, T] -> Optional[T]
Closes #325 Co-authored-by: Hugues Hoppe <[email protected]>
1 parent bacbd53 commit c2c1320

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

pdoc/__init__.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,9 @@ def maybe_replace_reprs(a):
12741274
# Union[T, None] -> Optional[T]
12751275
if (getattr(a, '__origin__', None) is typing.Union and
12761276
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__))))
12791280
return force_repr(f'Optional[{t}]')
12801281
# typing.NewType('T', foo) -> T
12811282
module = getattr(a, '__module__', '')
@@ -1285,10 +1286,8 @@ def maybe_replace_reprs(a):
12851286
if module.startswith('nptyping.'):
12861287
return force_repr(repr(a))
12871288
# Recurse into args
1288-
try:
1289+
if hasattr(a, 'copy_with') and hasattr(a, '__args__'):
12891290
a = a.copy_with(tuple([maybe_replace_reprs(arg) for arg in a.__args__]))
1290-
except Exception:
1291-
pass # Not a typing._GenericAlias
12921291
return a
12931292

12941293
return str(inspect.formatannotation(maybe_replace_reprs(annot)))
@@ -1393,7 +1392,7 @@ def return_annotation(self, *, link=None) -> str:
13931392
s = annot
13941393
else:
13951394
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)\)',
13971396
r'\g<str>', s)
13981397
s = s.replace(' ', '\N{NBSP}') # Better line breaks in html signatures
13991398

0 commit comments

Comments
 (0)